ListView getChildAt 为可见子项返回 null [英] ListView getChildAt returning null for visible children

查看:19
本文介绍了ListView getChildAt 为可见子项返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 listview/getChildAt 方法中得到了一些奇怪的行为.

I'm getting some strange behavior from a listview/the getChildAt method.

我有一个已在数据库中更改的图标的 HashSet,iconsToUpdate.我想遍历可见行以查看是否需要更新它们的任何图标以反映新图标.我不需要测试当前不在视图中的图标,因为它们会在渲染时正确绘制.

I have a HashSet, iconsToUpdate, of icons that have been changed in the database. I want to iterate over the visible rows to see if any of their icons need to be updated to reflect the new icons. I don't need to test the icons not currently in view as they will be drawn properly when rendered.

我的问题是 getChildAt 似乎不应该返回 null.我知道 getChildAt 只能返回当前可见的视图,但它为某些可见行返回 null.

My problem is that getChildAt is returning null when it seems like it shouldn't. I know that getChildAt can only return views that are currently visible, but it is returning null for some of the visible rows.

这是我遍历可见行的代码:

Here is my code that iterates over the visible rows:

Logger.debug("First visible index: " + f_listView.getFirstVisiblePosition());
Logger.debug("Last visible index: " + f_listView.getLastVisiblePosition());
for (int i = f_listView.getFirstVisiblePosition(); i <= f_listView.getLastVisiblePosition(); i++) {
    String tag = "asdf"; // Remove when bug is fixed.
    if (f_listView == null) {
        Logger.debug("f_listView is null");
    } else if (f_listView.getChildAt(i) == null) {
        Logger.debug("Child at index " + i + " is null");
    } else {
        tag = (String) f_listView.getChildAt(i).getTag();
        Logger.debug("Successful at index " + i + ", tag is: " + tag);
    }
    if (iconsToUpdate.contains(tag)) {
        setIcon(i, f_aim.getInHouseIcon(tag));
    }
}

这是与此循环的运行对应的日志:

Here is the log corresponding to a run of this loop:

D/...: First visible index: 3
D/...: Last visible index: 8
D/...: Successful at index 3, tag is: ...
D/...: Successful at index 4, tag is: ...
D/...: Successful at index 5, tag is: ...
D/...: Child at index 6 is null
D/...: Child at index 7 is null
D/...: Child at index 8 is null

应该注意的是,第一个和最后一个可见索引被正确报告,因为我在运行它时正在查看第 3-8 行.第 6、7、8 行正在正确呈现.如果它们为空,它们如何显示?

It should be noted that the first and last visible indexes are being correctly reported, as I am viewing rows 3-8 when I run this. Rows 6, 7, 8 are being rendered properly. How are they being displayed if they are null?

另外,我不知道这是否重要,但是当我在列表视图的顶部时,第 5 行是最后可见的行.

Also, I do not know if this is important, but row 5 is the last visible row when I am at the top of the listview.

有关为什么这些行被返回为空的任何信息将不胜感激.

Any info as to why these rows are being returned as null would be greatly appreciated.

谢谢!

推荐答案

listView.getChildAt(i) 在 0 是第一个可见行而 (n-1) 是最后一个可见行(其中 n 是您看到的可见视图).

listView.getChildAt(i) works where 0 is the very first visible row and (n-1) is the last visible row (where n is the number of visible views you see).

获取最后一个/第一个可见返回您拥有的 dataAdapter 中的位置.所以你因为你从位置 3 开始,看起来像是 6 个可见视图,那么当位置 6-8 时,你得到空值.

The get last/first visible return the position in the dataAdapter you have. So you since you start at position 3, with what looks like 6 visible views, that's when get for positions 6-8 you get null.

在您的示例中,getChildAt(0) 将返回位置 3.我通常做的是将位置存储在我的视图中,以便稍后在需要值时在我的 dataAdapter 上查找.

In your example getChildAt(0) would return position 3. What I usually do is store the position on my views so I can lookup on my dataAdapter later if I need values.

我认为你的 for 循环应该是这样的:

I think your for loop should look like this:

for (int i = 0; i <= f_listView.getLastVisiblePosition() - f_listView.getFirstVisiblePosition(); i++) 

希望这会有所帮助.

这篇关于ListView getChildAt 为可见子项返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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