How to find out if ListView has scrolled to top Most position?

后端 未结 10 821
情歌与酒
情歌与酒 2020-12-29 03:09

I have a ListView, first its scrolled down, now when we scroll up,it reach top most point. I want to detect that .Is there any way?I am developing application with api level

相关标签:
10条回答
  • 2020-12-29 03:48

    You will need to check what is the first visible position then applying Graeme's solution to see if the first visible listview item is at the top position.

    Something like lv.getFirstVisiblePosition() == 0 && (lv.getChildCount() == 0 || lv.getChildAt(0).getTop() == 0)

    0 讨论(0)
  • 2020-12-29 03:49

    edit

    See comments below as to why, especially on different API versions (esp later ones), this isn't a foolproof way to see if you're list is at the top (padding etc). However, it does give you a start for a solution on devices below API 14:

    private boolean listIsAtTop()   {   
        if(listView.getChildCount() == 0) return true;
        return listView.getChildAt(0).getTop() == 0;
    }
    

    As far as my implementation years ago - this worked perfectly at the time.

    0 讨论(0)
  • 2020-12-29 03:50

    You can use an OnScrollListener to be notified the position 0 is now visible. Use the onScrollmethod.

    0 讨论(0)
  • 2020-12-29 03:52

    My friends, combining Graeme's answer with the onScroll method...

    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
    
        }
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if(firstVisibleItem == 0 && listIsAtTop()){
                swipeRefreshLayout.setEnabled(true);
            }else{
                swipeRefreshLayout.setEnabled(false);
            }
        }
    });
    
    
    private boolean listIsAtTop()   {
        if(listView.getChildCount() == 0) return true;
        return listView.getChildAt(0).getTop() == 0;
    }
    
    0 讨论(0)
  • 2020-12-29 03:53

    Graeme's answer is close but is missing something that user2036198 added, a check for getFirstVisiblePosition(). getChildAt(0) doesn't return the very first view for the very first item in the list. AbsListView implementations don't make a single view for every position and keep them all in memory. Instead, view recycling takes effect to limit the number of views instantiated at any one time.

    The fix is pretty simple:

    public boolean canScrollVertically(AbsListView view) {
        boolean canScroll = false;
    
        if (view != null && view.getChildCount() > 0) {
            // First item can be partially visible, top must be 0 for the item
            canScroll = view.getFirstVisiblePosition() != 0 || view.getChildAt(0).getTop() != 0;
        }
    
        return canScroll;
    }
    

    For best results on ICS or higher, always use ViewCompat from the v4 support library or View.canScrollVertically(). Use the above method on lower API levels as ViewCompat always returns false for canScrollVertically() and canScrollHorizontally() below ICS.

    0 讨论(0)
  • 2020-12-29 03:56

    This question is old but I have a solution that works perfectly and it is possible that works for someone looking for a solution.

    int limitRowsBDshow = 10; //size limit your list
    listViewMessages.setOnScrollListener(new AbsListView.OnScrollListener() {
            int counter = 1;
            int currentScrollState;
            int currentFirstVisibleItem;
            int currentVisibleItemCount;
            int currentTotalItemCount;
    
    
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                this.currentScrollState = scrollState;
                this.isScrollCompleted();
            }
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    
                this.currentFirstVisibleItem = firstVisibleItem;
                this.currentVisibleItemCount = visibleItemCount;
                this.currentTotalItemCount = totalItemCount;
            }
    
            private void isScrollCompleted() {
                if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
                    /*** detect if there's been a scroll which has completed ***/
    
                    counter++;
                    if (currentFirstVisibleItem == 0 && currentTotalItemCount > limitRowsBDshow - 1) {
                         linearLay20msgMas.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    

    This code is found a time ago (here StackOverflow). But I can not find this to mention

    0 讨论(0)
提交回复
热议问题