Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out only completely visible list items in android- list view

I want to find out the position or ids related to a ListView's items: only those ones which are completely visible on the screen.
Using listview.getFirstVisibleposition and listview.getLastVisibleposition takes partial list items into account.

like image 866
Priya Mall Avatar asked Dec 12 '25 16:12

Priya Mall


1 Answers

I followed a little bit similar approach as suggested by Rich, to suit my requirement which was to fetch completely visible items on screen when List View is scrolled every time.

This is what i did

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    //Loop to get tids of all completely visible List View's item scrolled on screen
    for (int listItemIndex = 0; listItemIndex <= getListView().getLastVisiblePosition() - getListView().getFirstVisiblePosition(); listItemIndex++) {

        View listItem = getListView().getChildAt(listItemIndex);
        TextView tvNewPostLabel = (TextView) listItem.findViewById(R.id.tvNewPostLabel);
        if (tvNewPostLabel != null && tvNewPostLabel.getVisibility() == View.VISIBLE) {
            int listTid = (int) tvNewPostLabel.getTag();
            if (listItem.getBottom() < getListView().getHeight()) {//If List View's item is not partially visible
                listItemTids.add(listTid);
            }
        }
    }
}
like image 173
Priya Mall Avatar answered Dec 15 '25 10:12

Priya Mall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!