Dynamically adding Views in a RecyclerView only to current item

前端 未结 8 1964
一向
一向 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 13:00

    In your adapter class of your recyclerView, in the onBindViewHolder method, create another adapter and do the same methods for your new adapter.

    The hierarchy will be,

    mainRecyclerView -> item1(->childRecyclerView1) , item2(->childRecyclerView2), item3(->childRecyclerView3)

    This way you can achieve what you want without wrong values to be viewed on wrong items.

    0 讨论(0)
  • 2020-12-20 13:05

    This was an old question of mine. A bounty was placed on it, hence the surge of popularity and the multiple new and irrelevant answers.

    As stated in both my comment to this answer and @CQM's comment below my original question, the answer is to override the onViewRecycled() method and perform any needed operations there. This method is called when a view is recycled, and any cleanup operations can be done here.

    Documentation on this method can be found here.

    In my case, it was a matter of deleting the invisible TextView's attached to the view. The text itself had been deleted, but the view remained. If many invisible TextView's accumulate on the view and aren't properly recycled when scrolling, the scroll will begin to lag.

    0 讨论(0)
提交回复
热议问题