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

后端 未结 10 984
情歌与酒
情歌与酒 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: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

提交回复
热议问题