Recyclerview with multiple dynamic views being inflated inside row

十年热恋 提交于 2019-12-06 14:09:36

问题


I'm getting my data via an API that supplies me with an array of rows for my RecyclerView. Inside each of these rows there is an array of elements which I want to add dynamically to each of my RecyclerView rows. Each element corresponds to a view. So for example I might get a title, then an image, then text. Or I might get just an image. Or a title and text. These are just some of the elements, there are more and maybe hundreds of different possible combinations. For this reason it's not viable to create and inflate different row types as you would normally do if you need just a few different types of rows.

I tried to inflate and attach my views to the row on the onBindViewHolder method, but this caused the elements to be added again and again everytime that method was called.

I have searched around and have found no similar questions to what I am after, and I was wondering if there was a nice clean and elegant way of achieving what I need.


回答1:


I managed to achieve what I was looking for using the following method I had already tried:

I tried to inflate and attach my views to the row on the onBindViewHolder method, but this caused the elements to be added again and again everytime that method was called.

To prevent the views to being continuously added everytime a view was recycled, I overrided the onViewRecycled method, and removed all the views from the linear layout inside the row.

So far, I haven't had any problems with this method, but it's probably not the most efficient.



来源:https://stackoverflow.com/questions/31164051/recyclerview-with-multiple-dynamic-views-being-inflated-inside-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!