如何得到一个ListView项的看法? [英] How to get the view of a ListView item?

查看:118
本文介绍了如何得到一个ListView项的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表视图(A和B)与同类型的项目(一类我创建)

I have two ListViews (A and B) with items of the same type (a class I created)

当我点击从A项目,它增加了基于B这个对象,如果我再次点击它删除。 只有当一个项目被选中,我更改使用view.setBackgroundColor(myColor)。它的背景

When I click on an item from A, it adds this object on B and if I click again it removes it. Only that when an item is selected, I change its background using view.setBackgroundColor(myColor).

我希望能够删除名单B(它的工作)的项目,但我想也重新设置背景颜色。我无法弄清楚如何得到这个项目,我删除的观点。

I want to be able to remove the item from list B (it works), but I want also to reset the background color. I can't figure out how to get the view of this item I'm removing.

任何想法?

推荐答案

有没有保证,任何特定的ListView项甚至将有一个观点,在任何给定的时间。如果该项目是目前屏幕外,那么它可能有一番景致。由于特定的项目可能不会有一个看法,它可能没有任何意义,试图获得该项目的看法。

There's no guarantee that any specific ListView item will even have a view at any given time. If the item is currently off-screen, then it may not have a view. Since a specific item might not have a view, it might not make any sense to try to get the item's view.

除此之外,因为ListView的方式创建和重用的意见,你会看到一些奇怪的,不良的效果,如果你干脆直接修改意见。通过列表中的用户滚动,即成为可见的项目将错误地结束了那些可见的部分之外倒下相同背景的其他​​项目。

Beyond that, because of the way ListView creates and reuses views, you'll see some odd, undesirable effects if you simply modify the views directly. As the user scrolls through the list, items that become visible will incorrectly end up with the same backgrounds as other items that have fallen outside the visible portion.

我不知道接下来是实现你的功能,因为我不知道重建列表中的更改后的成本的最佳途径。这里的(可能是幼稚)的方式,我会做这样的:

I don't know whether what follows is the best way to implement your functionality because I don't know the cost of rebuilding the list after a change. Here's the (probably naive) way I would do this:

  1. 添加另一个布尔成员到您的数据对象,像 isInSecondList
  2. 覆盖 getView()的适配器。在 getView(),将背景设置为正常或突出显示取决于项目的 isInSecondList ,<值了/ LI>
  3. 当一个项目被添加或从第二个列表中删除,更新数据对象以反映更改,然后调用适配器的 notifyDataSetChanged()
  1. Add another boolean member to your data object, something like isInSecondList.
  2. Override getView() in the Adapter. In getView(), set the background to either normal or highlighted depending on the the value of the item's isInSecondList.
  3. When an item is added or removed from the second list, update the data object to reflect the change, then call the Adapter's notifyDataSetChanged().

这篇关于如何得到一个ListView项的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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