getChildAt方法是ListView父类ViewGroup的方法,参数是ListView的下标,获取到ListView的item的view
方法的返回值是本页面可见的item的view(ListView的回收机制,不可见的item是拿不到的)
正确的用法是View view = listview.getChildAt(position - listview.getFirstVisiblePosition());
position是ListView想要拿到的item下标,getFirstVisiblePosition()是当前可见的第一个item。得到的即为ListView的position位置的view
源码方法及其解释:/** * Returns the view at the specified position in the group. * * @param index the position at which to get the view from * @return the view at the specified position or null if the position * does not exist within the group */ public View getChildAt(int index) { //ViewGroup类的方法,ListView的父类 if (index < 0 || index >= mChildrenCount) { return null; } return mChildren[index]; }
/** * Returns the position within the adapter's data set for the first item * displayed on screen. * * @return The position within the adapter's data set */ public int getFirstVisiblePosition() { //AdapterView类的方法,ListView的父类 return mFirstPosition; }