ListView自定义后,在onItemClick中getChildAt返回null问题
在ListView中,使用getChildAt(index)的取值,只能是当前可见区域(列表可滚动)的子项!
1、所以如果想获取前部的将会出现返回Null值问题;
2、getChildCount跟getCount获取的值将会不一样(数量多时);
3、如果使用了getChildAt(index).findViewById(...)设置值的话,滚动列表时值就会改变了。
需要使用getFirstVisiblePosition()获得第一个可见的位置,在用当前的position-getFirstVisiblePosition(),再用getChildAt取值!
如果你硬是不想用 notifyDataSetChanged() 方法的话.........................................................................
---------------------------------------------------------------------
getChildAt(0) , 得到 getFirstVisiblePosition() 处的 view.
getChildAt(getChildCount() - 1), 得到最后一个可见的 view.
final int childIndex = position - getFirstVisiblePosition();
if (childIndex >= 0 && childIndex <listview.getChildCount())
{
// 更换 item 的图片背景.
}
else
{
// 什么也不做 - 当view变成可见时, adapter.getView() 方法会被调用的(在getView 方法里更换图片背景...).
}
本文介绍了解决ListView中使用getChildAt方法时遇到返回null的问题,详细解释了getChildAt方法的工作原理,并提供了一种有效的方法来确保始终能够正确获取ListView中的视图组件。
Android UI界面 关于Listview的getChildAt方法返回空值问题,该怎样解决?&spm=1001.2101.3001.5002&articleId=82879833&d=1&t=3&u=3833758d8de34557923875426bb51384)
1271

被折叠的 条评论
为什么被折叠?



