Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a my ListView has scrollable number of items?

How do you know if your ListView has enough number of items so that it can scroll?

For instance, If I have 5 items on my ListView all of it will be displayed on a single screen. But if I have 7 or more, my ListView begins to scroll. How do I know if my List can scroll programmatically?

like image 834
Ragunath Jawahar Avatar asked Jun 23 '12 22:06

Ragunath Jawahar


People also ask

Is a ListView scrollable?

ListView itself is scrollable.

Is a ViewGroup that displays a list of scrollable items?

ListView : is a view group that displays a list of scrollable item. GridView : is a ViewGroup that displays items in two-dimensional scrolling grid.

How do I check my scroll position in flutter?

I used NotificationListener that is a widget that listens for notifications bubbling up the tree. Then use ScrollEndNotification , which indicates that scrolling has stopped. For scroll position I used _scrollController that type is ScrollController .

How do I make ListView scroll smoothly?

The key to a smoothly scrolling ListView is to keep the application's main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread. To test the status of your app, you can enable StrictMode .


2 Answers

Diegosan's answer cannot differentiate when the last item is partially visible on the screen. Here is a solution to that problem.

First, the ListView must be rendered on the screen before we can check if its content is scrollable. This can be done with a ViewTreeObserver:

ViewTreeObserver observer = listView.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            if (willMyListScroll()) {
                 // Do something
            } 
        }
    });

And here is willMyListScroll():

boolean willMyListScroll() {   
    int pos = listView.getLastVisiblePosition();
    if (listView.getChildAt(pos).getBottom() > listView.getHeight()) {
        return true;
    } else {
        return false;
    }
}
like image 76
Mike Ortiz Avatar answered Jan 01 '23 00:01

Mike Ortiz


As per my comment on Mike Ortiz' answer, I believe his answer is wrong:

getChildAt() only counts the children that are visible on screen. Meanwhile, getLastVisiblePosition returns the index based on the Adapter. So if the lastVisiblePosition is 8 because it's the 9th item in the list and there are only 4 visible items on screen, you're gonna get a crash. Verify it by calling getChildCount() and see. The indices for getChildAt are not the same as the indices for the data set. Check this out: ListView getChildAt returning null for visible children

Here's my solution:

public boolean isScrollable() {
        int last = listView.getChildCount()-1; //last visible listitem view
        if (listView.getChildAt(last).getBottom()>listView.getHeight() || listView.getChildAt(0).getTop()<0) { //either the first visible list item is cutoff or the last is cutoff
                return true;
        }
        else{
            if (listView.getChildCount()==listView.getCount()) { //all visible listitem views are all the items there are (nowhere to scroll)
                return false;
            }
            else{ //no listitem views are cut off but there are other listitem views to scroll to
                return true;
            }
        }
    }
like image 22
jacosta Avatar answered Dec 31 '22 23:12

jacosta