Recyclerview with multiple dynamic views bloated inside a row

I get my data through an API that supplies me with an array of strings for my RecyclerView. Inside each of these lines there is an array of elements that I want to dynamically add to each of the RecyclerView lines. Each item corresponds to a view. So, for example, I can get the title, then the image, then the text. Or I can only get the image. Or title and text. These are just a few of the elements; there are more and perhaps hundreds of different possible combinations. For this reason, it is not practical to create and inflate different types of lines, as usual, if you need only a few different types of lines.

I tried to inflate and bind my views to the line of the onBindViewHolder method, but this caused the elements to be added again and again every time this method is called.

I searched around and did not find similar questions to what I needed, and I was wondering if there is a good clean and elegant way to achieve what I need.

+1
source share
1 answer

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

I tried to inflate and bind my views to the line of the onBindViewHolder method, but this caused the elements to be added again and again every time this method is called.

To prevent the view from constantly adding each time the view was redesigned, I overridden the onViewRecycled method and removed all views from the line layout within the row.

So far, I have not had any problems with this method, but it is probably not the most effective.

+2
source

Source: https://habr.com/ru/post/1233855/


All Articles