Android:获取列表视图中可见子项的计数

发布于 2024-11-25 01:58:16 字数 384 浏览 2 评论 0原文

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

我有一个列表视图,其中的信息链接到可以随时更改的数据库。当数据库更改时,我会发出一个广播,通知处理列表视图的 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 技术交流群。

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

发布评论

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

评论(3

像极了他 2024-12-02 01:58:16

这是获取可见子项数量的快速方法:

int visibleChildCount = (listView1.getLastVisiblePosition() - listView1.getFirstVisiblePosition()) + 1;

This is a quick way to get visible children count:

int visibleChildCount = (listView1.getLastVisiblePosition() - listView1.getFirstVisiblePosition()) + 1;
我要还你自由 2024-12-02 01:58:16

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

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....
    }
}

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....
    }
}
绝對不後悔。 2024-12-02 01:58:16

参考上面 greg7gkb 的评论 - 只是想指出,如果有人使用这个,它会让你倒数一。应该是

(listView1.getLastVisiblePosition() - listView1.getFirstVisiblePosition()) + 1

这样,如果最后一个可见的是 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

(listView1.getLastVisiblePosition() - listView1.getFirstVisiblePosition()) + 1

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.

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