ArrayAdapter的getView方法是在ListView中插入自定义视图的最佳方法吗?

发布于 2024-12-12 00:00:55 字数 242 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强辩 2024-12-19 00:00:55

当您回收视图时,您需要意识到您正在处理的当前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.

难得心□动 2024-12-19 00:00:55

关于 convertView 也许 Commonsware 书籍中的部分可能对您有帮助(页面107 如果你想跳)。

Regarding convertView maybe this part from the Commonsware books might help you(page 107 if you want to jump).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文