ListView getChildCount 以及getChildAt 坑 误区指南

今天调试的时候,才知道。
原来listview 的 getChildCount 取得是当前可先的list item 的个数,而不是整个listview 的count.

整个listview 的数量应该是 getCount

/**
 * @return The number of items owned by the Adapter associated with this
 *         AdapterView. (This is the number of data items, which may be
 *         larger than the number of visible views.)
 */
@ViewDebug.CapturedViewProperty
public int getCount() {
    return mItemCount;
}

getChildCount

/**
 * Returns the number of children in the group.
 *
 * @return a positive integer representing the number of children in
 *         the group
 */
public int getChildCount() {
    return mChildrenCount;
}


/**
 * 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) {
    if (index < 0 || index >= mChildrenCount) {
        return null;
    }
    return mChildren[index];
}

总结:
getChildCount 和 getChildAt 都是取得listView当前可见的item.

posted @ 2017-07-03 16:42  有点理想的码农  阅读(643)  评论(0编辑  收藏  举报