This is because views are returned and reused.
Therefore, when a view returns, it retains the properties of the "old" view, unless you change them again. Therefore, when you scroll down to number 12, the view that is used to store number 1 is processed (since it can no longer be seen on the screen) and used to create number 12. That's why the blue color is on number 12.
When an element, for example, has clicked, you need to save the "clicked" value in your POJO object. Then, when the element is drawn, check this value and set the correct image / background color depending on this value.
I did this in the code below, so it should give you a general idea of what to do:
@Override public void onBindViewHolder(ViewHolder holder, final int position) { TextView title = (TextView) holder.view.findViewById(R.id.title); final TextView desc = (TextView) holder.view.findViewById(R.id.desc); final ImageView imageView = (ImageView) holder.view.findViewById(R.id.imageView); final MyPojo pojo = pojos.get(position); title.setText(pojo.getTitle()); if(!pojo.clicked) { desc.setText(pojo.getDesc()); imageView.setImageResource(pojo.getImage()); desc.setBackgroundColor(Color.argb(0,0,0,0)); } else { desc.setText("clicked"); desc.setBackgroundColor(Color.BLUE); imageView.setImageResource(R.drawable.heart_red); } imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { pojo.clicked = true; desc.setText("clicked"); desc.setBackgroundColor(Color.BLUE); imageView.setImageResource(R.drawable.heart_red); } }); }
And I added a "clicked" boolean to the MyPojo class.
public class MyPojo { String title; String desc; int image; boolean clicked; }
Moonbloom
source share