Android 列表视图视图持有者。何时使用,何时不使用
我有一个带有自定义列表适配器的 ListView。在 getView() 方法中,我使用 ViewHolder“模式”,如 ListView14.java 的 API 演示中所示。当我第一次渲染列表时,它似乎加载正确。然而,我遇到的问题是,当我滚动列表时,我看到列表的数据显示在错误的行中(例如,应该位于第 10 行的 TextView 却显示在第 2 行中) )。但是,当我不使用视图持有者,而是每次调用 findViewById() 时,列表视图都会正确呈现。
I have a ListView with a custom list adapter. In the getView() method, am using the ViewHolder 'pattern' as shown in the API Demos for ListView14.java. When i first render the list it seems to load correctly. However, the issue i'm running into is that when i scroll the list, i'm seeing the data for the list show up in the wrong rows (i.e. a TextView that should be in row 10 is showing up in row 2 for example). However, when I do not use the viewholder, and instead call findViewById() every time, then the list view renders correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最有可能的是,您不正确地回收了行,因此您正在操作的 ViewHolders 不是您要返回的行的正确值。
这是我的一本书中的免费摘录,其中详细介绍了行回收 - 也许它将帮助您确定哪里出了问题。
Most likely, you are improperly recycling your rows, such that the
ViewHolders
you are manipulating are not the right ones for the row you are returning.Here is a free excerpt from one of my books that goes into more about row recycling -- perhaps it will help you identify where things are going wrong.
在您的自定义适配器类中使用访问说明符添加 ViewHolder
在 获取视图方法 中
添加 ViewHolder 重要: 我们不应该为除 Viewholder 之外的视图对象设置任何标签目的
in Your Custom Adapter class add ViewHolder using Access Specifiers
In Get View method
Important : And We should not set Any Tag for view object except Viewholder Object
所以我想我在这里发现了真正的问题。当您为每一行动态设置布局参数时,您需要确保针对所有条件都执行此操作。我的问题是,如果它是第一行,我设置一个布局参数(如填充或边距等),但如果它是中间行,我没有明确设置这些参数,认为它只会使用膨胀的内容通过视图充气机。这解释了为什么当我每次放大视图时它都会起作用。这是之前和之后的内容。之后:
之前:
之后:`
so i think i discovered the real issue here. when you set layout parameters on the fly for each row, you need to make sure you do it for all conditions. my problem was that if it was the first row, i set a layout param (like padding or margins etc), but then if it was a middle row, i didn't explicitly set those params thinking that it would just use what was inflated by the view inflater. This explains why it worked when i inflated the view each time. Here is a before & after:
BEFORE:
AFTER:`