我如何创建一个列表,当你到达列表的末尾时,我会收到通知,这样我就可以加载更多的项目?
当前回答
这个问题的关键是检测load-more事件,启动数据异步请求,然后更新列表。还需要一个带有加载指示器和其他装饰器的适配器。事实上,在一些极端情况下,这个问题非常复杂。只有OnScrollListener实现是不够的,因为有时项目不会填满屏幕。
我写了一个个人包,它支持无限列表的RecyclerView,也提供了一个异步加载器实现AutoPagerFragment,这使得它很容易从多页源获取数据。它可以将任何页面加载到自定义事件的RecyclerView中,而不仅仅是下一页。
这是地址:https://github.com/SphiaTower/AutoPagerRecyclerManager
其他回答
到目前为止,我见过的最好的解决方案是在FastAdapter库中用于回收器视图。它有一个EndlessRecyclerOnScrollListener。
下面是一个示例用法:EndlessScrollListActivity
一旦我使用它无休止的滚动列表,我已经意识到设置是一个非常强大的。我强烈推荐。
可能有点晚了,但下面的解决方案在我的情况下非常有用。 在某种程度上,所有你需要做的是添加到你的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");
}
});
当页脚可见时,此事件会触发一次,效果非常好。
这个问题的关键是检测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.
下面是一个解决方案,它也可以很容易地在ListView加载时显示加载视图。
你可以在这里看到这些类:
https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/helper/ListViewWithLoadingIndicatorHelper.java - Helper,使它可以使用功能而不扩展从SimpleListViewWithLoadingIndicator。
https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/listener/EndlessScrollListener.java -当用户即将到达ListView的底部时,监听器开始加载数据。
https://github.com/CyberEagle/OpenProjects/blob/master/android-projects/widgets/src/main/java/br/com/cybereagle/androidwidgets/view/SimpleListViewWithLoadingIndicator.java - EndlessListView。您可以直接使用这个类,也可以从它扩展。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- 为什么元组可以包含可变项?
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- 不区分大小写的“in”
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何分裂()一个分隔字符串到一个列表<字符串>