N
N
NameOf Var2017-08-17 01:20:00
Java
NameOf Var, 2017-08-17 01:20:00

How to correctly describe the handling of keypresses in RecyclerView?

There is a RecyclerView with this template:
q74hEU0mR-Wz3wskH8cZjg.png
For it, I made this model:

class PostModel {

    public String title;
    public String subtitle;
    public String mainText;

    public int likes;
    public int dislikes;
    public final Random rand = new Random();

    public PostModel(String newTitle, String newSubtitle, String newMainText) {
        this.title = newTitle;
        this.subtitle = newSubtitle;
        this.mainText = newMainText;
        this.likes = rand.nextInt(1000);
        this.dislikes = rand.nextInt(1000);
    }
}

It is required to describe an event handler that, when the like button is clicked, would increase the counters of likes or dislikes.
I tried to do it in the adapter in the onBindViewHolder method, but when I clicked on any of the like buttons, only the bottommost element was enlarged. How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Chvarkov, 2017-08-17
@hax

I use the following in such cases:
Inside the onBindViewHolder, you hang an OnClickListener on the buttons. In it, from the list of elements, you select the element with the number holder.getAdapterPosition(), in it you change the value of likes / dislikes and notify that the data has changed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question