Check if android listview is scrolled down to the last item

▼魔方 西西 提交于 2021-01-28 00:30:24

问题


I have a listview in which there are several items. I want to check whether the list is scrolled down to the last item, in which case I want to run another method.How to do that?


回答1:


at first set isLoading = false; in the constructore or onCreate method

  mListView.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (mListView.getAdapter() == null)
                return ;

            if (mListView.getAdapter().getCount() == 0)
                return ;

            int l = visibleItemCount + firstVisibleItem;
            if (l >= totalItemCount && !isLoading) {
                // It is time to add new data. We call the listener
                isLoading = true;
                loadData();

            }
        }
    });

and in function loadData you do something like:

 public void loadData() {

    // send your request
   // receive it by callback or asynctask
   yourDataList.addAll(newItems);
   adapter.notifyDataSetChanged();
   isLoading = false;
}



回答2:


Have a look at listView.getLastVisiblePosition().



来源:https://stackoverflow.com/questions/25350137/check-if-android-listview-is-scrolled-down-to-the-last-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!