简体   繁体   中英

Android ListView automatically Load more data when scroll to the top

There was a question about Android ListView automatically Load more data when scroll to the Bottom . But what I need is exactly opposite of that question. I've reversed the first answer but it didn't work and if the list view is empty none of scrolling events called in the first answer. So help me to detect scroll up event in android listview to update it.

Below logic will work for loading more data when you scroll to the top of the listview.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM