Android:获取列表视图中可见子项的计数 [英] Android: Getting a count of the visible children in a listview

查看:22
本文介绍了Android:获取列表视图中可见子项的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法计算可见列表视图子项的数量?

Is there are way to get a count of the number of visible listview children?

我有一个列表视图,其中的信息链接到一个可以随时更改的数据库.当数据库更改时,我发送一个广播通知处理列表视图的 ui 类.然后更新与更改数据相关的子元素.我通过给每个列表视图项一个标签来实现这一点,然后遍历列表视图以找到与广播中的标签匹配的行.

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.

我只想迭代可见的孩子.我不需要手动更新不可见的视图,因为它们会在创建时反映新数据.我目前从 listView.getfirstVisiblePosition() 迭代到 listView.getChildCount().这总比没有好,因为我不检查可见行上方的行,但我也不想检查它们下方的行.

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.

我检查了 android 开发者的 listView 页面,没有找到任何东西.有谁知道我可以获取可见儿童数量的方法?

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?

谢谢!

推荐答案

listView.getLastVisiblePosition(),这是您要找的吗?如果不,通过子视图迭代...

listView.getLastVisiblePosition(), is this what you are looking for? if not, Iteration through child views...

int count = 0;

for (int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
    if (listView.getChildAt(i) != null)
    {
        count++;  // saying that view that counts is the one that is not null, 
                  // because sometimes you have partially visible items....
    }
}

这篇关于Android:获取列表视图中可见子项的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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