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

后端 未结 8 654
故里飘歌
故里飘歌 2020-12-30 04:00

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 si

8条回答
  •  鱼传尺愫
    2020-12-30 04:33

    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;
                }
            }
        }
    

提交回复
热议问题