ArrayAdapter的getView方法是在ListView中插入自定义视图的最佳方法吗?
我有一个 ListView,根据索引填充不同的视图。通过重写 getView 方法可以很好地工作,但我想知道这是否是实现此目的的最佳方法?我问的主要原因是,当我滚动时,某些视图似乎被搞砸了,例如我设置的背景错误。
如果有人可以解释 convertView
到底是什么以及实现它的正确方法,也许会有所帮助。因为我担心我的视图获得错误背景的原因是因为我使用的 convertView
进入函数并且它不正确。
I have a ListView
that I am filling with different views depending on the index. This works fine by overriding the getView
method, but I am wondering if this is the best way of accomplishing this? The main reason I ask is when I scroll some of the views seem to get screwed up, such as having the wrong background that I set.
Maybe it would help if someone could explain what convertView
is exactly and the correct way of implementing it. Because I am concerned that the reason why my views are getting the wrong background is because I am using the convertView
coming in to the function and it is not correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您回收视图时,您需要意识到您正在处理的当前convertView(当
convertView!= null
)已经处于某种状态(例如,对于不同的元素具有特定的背景)。因此,您需要确保相应地设置视图的每个部分,并且不假设任何默认值。这样想,当您向下滚动时,顶部视图将移出可见性并重新用作底部显示的新视图 - 如果您不对此视图进行任何更改,它将看起来与以前完全相同。
When you are recycling views you need to realise that the current convertView (when
convertView != null
) you are dealing with is already in a certain state (e.g. has a certain background for a different element). Therefore you need to make sure you set every part of the view accordingly and assume no default values.Think of it this way, when you are scrolling downwards, the top view is moved out of visibility and reused as the new view appearing at the bottom - if you don't change anything for this view, it will look exactly the same as before.
关于
convertView
也许 Commonsware 书籍中的这部分可能对您有帮助(页面107 如果你想跳)。Regarding
convertView
maybe this part from the Commonsware books might help you(page 107 if you want to jump).