简体   繁体   English

Android ListView 滚动到顶部时自动加载更多数据

[英]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 .有一个关于Android ListView 在滚动到底部时自动加载更多数据的问题。 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.所以帮我检测android listview中的向上滚动事件来更新它。

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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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