问题
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