Android 在 ListView 上实现无限滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12583419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implement Endless scroll on ListView
提问by ManiTeja
I would like to implement endless scroll on an android ListView component, that will add new items to the bottom of the listview when scrolled to the last element in the list.
我想在 android ListView 组件上实现无限滚动,当滚动到列表中的最后一个元素时,它将向列表视图的底部添加新项目。
回答by Pratik
implement the onscrolllistener()
实现 onscrolllistener()
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
private void isScrollCompleted() {
if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
/*** In this way I detect if there's been a scroll which has completed ***/
/*** do the work for load more date! ***/
if(!isLoading){
isLoading = true;
loadMoreDAta();
}
}
}
as you implement and add this listener to your listview this will detect for the end of listview as scroll position was at end of list just get more data. And during the loading data you need one flag to load data at once when the scroll position goes to end. So if data is loading and during that time you scroll up then scroll down then it will not get more data for dupplication.
当您实现并将此侦听器添加到您的列表视图时,这将检测列表视图的末尾,因为滚动位置位于列表末尾,只需获取更多数据。在加载数据期间,您需要一个标志来在滚动位置结束时立即加载数据。因此,如果数据正在加载,并且在此期间向上滚动然后向下滚动,则不会获得更多数据进行复制。
回答by Praveenkumar
Try with OnScrollListenerJust add the items to your ListView
in your
尝试使用OnScrollListener只需将项目添加到您ListView
的
public void onScroll(AbsListView view,
int firstVisible, int visibleCount, int totalCount) {
// Add the items
}
}
And, setting your adapter.notifyDataSetChanged()
will display the added items in your ListView
Here i give you some example that how to integrate the onScrollListener
with your ListView
并且,设置您adapter.notifyDataSetChanged()
将在您的ListView
此处显示添加的项目,我给您一些示例,说明如何将其onScrollListener
与您的 ListView集成