Skip to content Skip to sidebar Skip to footer

Listview Getfirstvisibleposition Getfirstvisibleposition Getcount Values

I have a custom ListView class (subclassed from ListView) and I need it to add a small padding to the bottom of the final view element so that it is not overlapped by a small bar t

Solution 1:

I'm not sure if it's the best solution, but I know that in one of the branches of a pull-to-refresh implementation for Android, the height of the list is compared against the total height of all the list items. From what you're saying, it sounds like that's the same information you're looking for in order to determine whether to apply extra padding or not.

The relevant parts of the implementation are in the following three methods:

@OverrideprotectedvoidonDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mHeight == -1) {  // do it only once
        mHeight = getHeight(); // getHeight only returns useful data after first onDraw()
        adaptFooterHeight();
    }
}

/**
 * Adapts the height of the footer view.
 */privatevoidadaptFooterHeight() {
    intitemHeight= getTotalItemHeight();
    intfooterAndHeaderSize= mFooterView.getHeight()
        + (mRefreshViewHeight - mRefreshOriginalTopPadding);
    intactualItemsSize= itemHeight - footerAndHeaderSize;
    if (mHeight < actualItemsSize) {
        mFooterView.setHeight(0);
    } else {
        inth= mHeight - actualItemsSize;
        mFooterView.setHeight(h);
        setSelection(1);
    }
}

/**
 * Calculates the combined height of all items in the adapter.
 * 
 * Modified from http://iserveandroid.blogspot.com/2011/06/how-to-calculate-lsitviews-total.html
 * 
 * @return 
 */privateintgetTotalItemHeight() {
    ListAdapteradapter= getAdapter();
    intlistviewElementsheight=0;
    for(inti=0; i < adapter.getCount(); i++) {
        ViewmView= adapter.getView(i, null, this);
        mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        listviewElementsheight+= mView.getMeasuredHeight();
    }
    return listviewElementsheight;
}

The full source code can be found here on GitHub.

Solution 2:

I had a similar issue when getFirstVisiblePosition() returned number greater than getCount(). Fixed it by calling listView.setAdapter(listView.getAdapter()).

Post a Comment for "Listview Getfirstvisibleposition Getfirstvisibleposition Getcount Values"