Android:获取列表视图中可见子项的计数
有没有办法获得可见列表视图子项的数量?
我有一个列表视图,其中的信息链接到可以随时更改的数据库。当数据库更改时,我会发出一个广播,通知处理列表视图的 ui 类。然后更新与改变的数据相关的子元素。我通过给每个列表视图项一个标签,然后迭代列表视图以查找与广播中的标签匹配的行来实现此目的。
我只想迭代可见的孩子。我不需要手动更新不可见的视图,因为它们在创建时会反映新数据。我目前从 listView.getfirstVisiblePosition() 迭代到 listView.getChildCount()。这总比没有好,因为我不检查可见行上方的行,但我也不想检查它们下方的行。
我检查了android开发者listView页面,没有发现任何东西。有人知道我可以计算可见儿童的数量吗?
谢谢!
Is there are way to get a count of the number of visible listview children?
I have a listview with info linked to a database that can change at any time. When the database is changed, I send out a broadcast notifying the ui class handling the list view. The child element relating to the changed data is then updated. I am achieving this by giving each listview item a tag, and then iterating over the listviews to find the row matching the tag from the broadcast.
I want to only iterate over the visible children. There is no need for me to manually update views that are not visible, as they will reflect the new data when they are created. I currently iterate from listView.getfirstVisiblePosition() to listView.getChildCount(). This is better than nothing, as I don't examine rows above the visible rows, but I don't want to examine the rows below them either.
I checked the android developers listView page and didn't find anything. Anyone know of a way I can get the count of visible children?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是获取可见子项数量的快速方法:
This is a quick way to get visible children count:
listView.getLastVisiblePosition(),这是您要找的吗?如果不,
通过子视图迭代...
listView.getLastVisiblePosition(), is this what you are looking for? if not,
Iteration through child views...
参考上面 greg7gkb 的评论 - 只是想指出,如果有人使用这个,它会让你倒数一。应该是
这样,如果最后一个可见的是 8,第一个可见的是 5,那么您将得到 (8-5)+1 = 4 显示:5、6、7 和 8。
看起来 A. Abiri 就在下面。
In reference to greg7gkb's comment above - just wanted to point out in case anyone is using this that it will make your count off by one. It should be
So, if the last visible was 8 and the first visible was 5, you would have (8-5)+1 = 4 showing:5,6,7, and 8.
It looks like A. Abiri got it right below.