Dynamically adding Views in a RecyclerView only to current item

前端 未结 8 2024
一向
一向 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:57

    First of all, can you share some more code please?

    Second, why would you want to dynamically add new views on fly? Why don't you use different VIEWTYPE or just have those view already on your layout and just make them visible/invisible or visible/gone? (I believe it will be more efficient this way).

    Let me remind you something about RecyclerView, yes when user is scrolling viewHolder are being reused (few of them can be created, even more than it needs to fill the screen). So if it happened that you added some views on "item A" and user scroll to "item Z", that viewHolder can be reused for that "item Z", hence the show up of the previously added views.

    How can you solve that?

    Well always check on every items if you need to add new views, if yes add them if not already added, else always remove those views (if present) to return to default viewHolder state (or whatever you call it).

    Hope this will help you.

提交回复
热议问题