Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView onClick problems

I have been working on RecyclerView and when a user clicks on an item I want the recyclerview panel to be changed. For simple testing purposes I am making the text of the recyclerview item red when clicked upon. The clicking properly but when I scroll the RecyclerView, other items randomly turn red. These random items are not clicked upon and are becoming red by themselves when I scroll. I think my problem should be in the onClick method.

public class interestAdapter extends RecyclerView.Adapter<interestAdapter.MyViewHolder> {
private LayoutInflater inflater;
private Context context;
public ArrayList<String> data = new ArrayList();
public static MyViewHolder.ViewHolderClick click;
//public static MyViewHolder.ViewHolderClick click;



public interestAdapter(Context context, ArrayList<String> objects) {
    this.context = context;
    inflater = LayoutInflater.from(context);
    for(int i = 0; i < objects.size(); i++) {
        data.add(objects.get(i));
    }
}

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.interestpanel, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;

}

public int getItemCount() {
    return data.size();
}

public void setClickListener(MyViewHolder.ViewHolderClick clicker) {
    this.click = clicker;
}

public void onBindViewHolder(MyViewHolder holder, int position) {
    String current = data.get(position);
    holder.category.setText(current);


}

static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { //implements View.onClickListener {
    TextView category;
    public MyViewHolder(View itemView) {
        super(itemView);
        //itemView.setOnClickListener(this);

        category = (TextView) itemView.findViewById(R.id.category);
        category.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

            MyViewHolder holder = new MyViewHolder(v);
            holder.category.setTextColor(Color.RED);

    }
    public TextView getCate() {
        return category;
    }

    public static interface ViewHolderClick {
        public void clicked(View view, int position);
    }


}

} `

like image 430
sganu Avatar asked May 30 '26 03:05

sganu


2 Answers

The problem is quite straight forward as we know recycler view recycle the view one you have changed the color of one view in the list view the color of that view remain of that color in your case red.So at the time of bind view holder you need to change the color back to normal ifs not clicked in other way you have to maintain the position of the clicked view and change the color accordingly.

like image 51
Piyush Avatar answered May 31 '26 17:05

Piyush


RecyclerView uses same row reference multiple times via myHolder object. Problem is at :

MyViewHolder holder = new MyViewHolder(v);  //this holder has multiple reference in recyclerView
holder.category.setTextColor(Color.RED);

As Piyush suggests correctly, maintain a field in your model for click (boolean). And render clicks with check on this boolean. update the boolean at your onclick

What I normally do is:

public void onBindViewHolder(final ContactViewHolder convosViewHolder, final int i) {

        convosViewHolder.name.setText(contacts.get(i).getName());
        convosViewHolder.phone.setText(contacts.get(i).getNumber());

        convosViewHolder.cv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //turn red here
        });
    }

You can try either. hope it helps.

like image 26
Karan Avatar answered May 31 '26 15:05

Karan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!