RecyclerView causes issue when recycling

后端 未结 3 649
深忆病人
深忆病人 2020-12-01 12:52

I have a list of items that I created using RecyclerView. When the user clicks on one of them I change the background color of that selected item. The problem i

3条回答
  •  粉色の甜心
    2020-12-01 13:30

    You should modify your logic assign the value inside the item (object) not the view:

    orderItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               orderItem.setSelected(xxxx);
            }
        });
    

    Then in your onBindViewHolder method you have to assing the color according to this value in the item.

    if (orderItem.isSelected()){
       viewHolder.orderItem.setBackgroundColor(xxxx);
    } else {
      viewHolder.orderItem.setBackgroundColor(xxxx);
    }
    

提交回复
热议问题