Android ListView automatically loads more data when scrolling up

There was a question about Android ListView automatically Load more data when scrolling to the bottom level . But what I need is the exact opposite of this question. I canceled the first answer, but it did not work, and if there are no empty scroll events in the list that are called in the first answer. So help me find the scroll event up in the android list to update it.

+4
source share
1 answer

Below logic will work to load more data when scrolling at the top of the list.

listview.setOnScrollListener(new AbsListView.OnScrollListener(){
    @Override
    public void onScrollStateChanged (AbsListView view,int scrollState){
    currentScrollState = scrollState;
      if (currentVisibleItemCount > 0 && currentScrollState == SCROLL_STATE_IDLE) {
        if (currentFirstVisibleItem == 0) {

            // getMessages(); //write what you want to do when you scroll up to the end of listview.

        }
      }
    }

    @Override
    public void onScroll (AbsListView view,int firstVisibleItem, int visibleItemCount,
    int totalItemCount){
     currentFirstVisibleItem = firstVisibleItem;
     currentVisibleItemCount = visibleItemCount;
     currentTotalItemCount = totalItemCount;
    }
    });

Hope this helps.

+1

Source: https://habr.com/ru/post/1544975/


All Articles