Dynamically adding Views in a RecyclerView only to current item

前端 未结 8 1962
一向
一向 2020-12-20 12:08

I\'m dynamically adding Views to my items in a RecyclerView. These added Views should only be related to the item which they\'re added to, but I\'m having a pro

8条回答
  •  有刺的猬
    2020-12-20 12:56

    Based on this:

    but those previously added Views are still there, but now on the wrong item.

    Basically, as per the RecyclerView documentation, You have to reset the views everytime inside the onBindViewHolder() method,

    so let say, you have a method that sets a view param if its your profile, so the code for the same goes as follows,

    if (list.get(position).getId()==PreferenceManager.getUserID())
    {
      // do some view change here
    setViewParam(true);
    }else
    {
      // reset the view change here
    setViewParam(false);
    }
    

    So what you're doing here is giving recycled ViewHolder a chance to reset. Do comment if you need help!

提交回复
热议问题