Recyclerview多个动态视图被充气内排 [英] Recyclerview with multiple dynamic views being inflated inside row

查看:96
本文介绍了Recyclerview多个动态视图被充气内排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过与行我RecyclerView阵列提供我一个API得到我的数据。在每个这些行有,我要动态地添加到我的每一个RecyclerView行元素的数组。每个元素对应的视图。例如,我可能会得到一个标题,然后图像,然后文本。或者,我可能会只是一个形象。或标题和文本。这些只是一些元件的,有更多的,或许数百个不同的可能的组合。出于这个原因,它不是可行的创建和膨胀不同的行类型,如果你只需要几种不同类型的行,你通常会做。

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.

我试图膨胀,附上我的意见,对onBindViewHolder方法行,但是这造成一再添加元素每次该方法被调用。

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.

推荐答案

我设法实现我一直在寻找使用下面的方法我已经试过:

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

我试图膨胀,附上我的意见,对onBindViewHolder方法行,但是这造成一再添加元素每次该方法被调用。

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.

要prevent的意见,正在不断补充每次视图被回收,我overrided的onViewRecycled方法,并移除了行内的线性布局。

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.

这篇关于Recyclerview多个动态视图被充气内排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆