我如何创建一个列表,当你到达列表的末尾时,我会收到通知,这样我就可以加载更多的项目?
当前回答
这个问题的关键是检测load-more事件,启动数据异步请求,然后更新列表。还需要一个带有加载指示器和其他装饰器的适配器。事实上,在一些极端情况下,这个问题非常复杂。只有OnScrollListener实现是不够的,因为有时项目不会填满屏幕。
我写了一个个人包,它支持无限列表的RecyclerView,也提供了一个异步加载器实现AutoPagerFragment,这使得它很容易从多页源获取数据。它可以将任何页面加载到自定义事件的RecyclerView中,而不仅仅是下一页。
这是地址:https://github.com/SphiaTower/AutoPagerRecyclerManager
其他回答
只是想为我的应用程序贡献一个解决方案。
它也基于OnScrollListener接口,但我发现它在低端设备上的滚动性能要好得多,因为在滚动操作期间不执行任何可见/总计数计算。
Let your ListFragment or ListActivity implement OnScrollListener Add the following methods to that class: @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //leave this empty } @Override public void onScrollStateChanged(AbsListView listView, int scrollState) { if (scrollState == SCROLL_STATE_IDLE) { if (listView.getLastVisiblePosition() >= listView.getCount() - 1 - threshold) { currentPage++; //load more list items: loadElements(currentPage); } } } where currentPage is the page of your datasource that should be added to your list, and threshold is the number of list items (counted from the end) that should, if visible, trigger the loading process. If you set threshold to 0, for instance, the user has to scroll to the very end of the list in order to load more items. (optional) As you can see, the "load-more check" is only called when the user stops scrolling. To improve usability, you may inflate and add a loading indicator to the end of the list via listView.addFooterView(yourFooterView). One example for such a footer view: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/footer_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center_vertical" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/progressBar1" android:padding="5dp" android:text="@string/loading_text" /> </RelativeLayout> (optional) Finally, remove that loading indicator by calling listView.removeFooterView(yourFooterView) if there are no more items or pages.
这个问题的关键是检测load-more事件,启动数据异步请求,然后更新列表。还需要一个带有加载指示器和其他装饰器的适配器。事实上,在一些极端情况下,这个问题非常复杂。只有OnScrollListener实现是不够的,因为有时项目不会填满屏幕。
我写了一个个人包,它支持无限列表的RecyclerView,也提供了一个异步加载器实现AutoPagerFragment,这使得它很容易从多页源获取数据。它可以将任何页面加载到自定义事件的RecyclerView中,而不仅仅是下一页。
这是地址:https://github.com/SphiaTower/AutoPagerRecyclerManager
可能有点晚了,但下面的解决方案在我的情况下非常有用。 在某种程度上,所有你需要做的是添加到你的ListView页脚,并为它创建addOnLayoutChangeListener。
http://developer.android.com/reference/android/widget/ListView.html addFooterView (android.view.View)
例如:
ListView listView1 = (ListView) v.findViewById(R.id.dialogsList); // Your listView
View loadMoreView = getActivity().getLayoutInflater().inflate(R.layout.list_load_more, null); // Getting your layout of FooterView, which will always be at the bottom of your listview. E.g. you may place on it the ProgressBar or leave it empty-layout.
listView1.addFooterView(loadMoreView); // Adding your View to your listview
...
loadMoreView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Log.d("Hey!", "Your list has reached bottom");
}
});
当页脚可见时,此事件会触发一次,效果非常好。
在Ognyan Bankov GitHub我发现了一个简单而有效的解决方案!
它利用了Volley HTTP库,使Android应用程序的网络更容易,更重要的是,更快。Volley可以通过开放的AOSP存储库获得。
给定的代码演示:
由HTTP分页请求填充的ListView。 NetworkImageView的使用。 “无尽”的ListView分页预读。
为了将来的一致性,我分叉了班科夫的回购。
I've been working in another solution very similar to that, but, I am using a footerView to give the possibility to the user download more elements clicking the footerView, I am using a "menu" which is shown above the ListView and in the bottom of the parent view, this "menu" hides the bottom of the ListView, so, when the listView is scrolling the menu disappear and when scroll state is idle, the menu appear again, but when the user scrolls to the end of the listView, I "ask" to know if the footerView is shown in that case, the menu doesn't appear and the user can see the footerView to load more content. Here the code:
的问候。
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(scrollState == SCROLL_STATE_IDLE) {
if(footerView.isShown()) {
bottomView.setVisibility(LinearLayout.INVISIBLE);
} else {
bottomView.setVisibility(LinearLayout.VISIBLE);
} else {
bottomView.setVisibility(LinearLayout.INVISIBLE);
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
推荐文章
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?
- 操作栏和新引入的工具栏有什么不同?
- 调整浮动动作按钮图标大小(fab)
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- android:在触摸移动时移动视图