转 https://www.jianshu.com/p/4ea7c2d95ecf
在Android开发中,RecyclerView算是使用频率非常广泛的组件了吧,在这里对RecyclerView比较常用的下拉刷新和上拉加载更多的功能实现做个记录,方便以后查看。在这里下拉刷新使用的是官方提供的SwipeRefreshLayout,然后上拉加载更多的功能使用的是第三方库BaseRecyclerViewAdapterHelper实现。
在项目build.gradle文件中添加以下代码导入依赖:
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46' implementation group: 'androidx.recyclerview', name: 'recyclerview', version: '1.1.0-alpha01'以上代码中RecyclerView的数据集使用假数据测试,并且模拟第一次上拉加载更多成功,第二次上拉加载失败,点击失败重试,最后加载完毕(即没有更多数据)
MainActivity的布局文件activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </androidx.constraintlayout.widget.ConstraintLayout>运行结果截图
运行结果加载更多的View布局是可以自定义的,自定义调用的方法:
mainAdapter.setLoadMoreView(new CustomLoadMoreView()); CustomLoadMoreView import com.chad.library.adapter.base.loadmore.LoadMoreView; public final class CustomLoadMoreView extends LoadMoreView { @Override public int getLayoutId() { return R.layout.view_load_more; } @Override protected int getLoadingViewId() { return R.id.load_more_loading_view; } @Override protected int getLoadFailViewId() { return R.id.load_more_load_fail_view; } @Override protected int getLoadEndViewId() { return R.id.load_more_load_end_view; } } 加载更多布局文件view_load_more.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/dp_40"> <LinearLayout android:id="@+id/load_more_loading_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:visibility="visible" android:orientation="horizontal"> <ProgressBar android:id="@+id/pb_footer" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" /> <TextView android:id="@+id/tv_footer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在加载中..." /> </LinearLayout> <FrameLayout android:id="@+id/load_more_load_fail_view" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> <TextView android:id="@+id/tv_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/brvah_load_failed"/> </FrameLayout> <FrameLayout android:id="@+id/load_more_load_end_view" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/brvah_load_end" android:textColor="@android:color/darker_gray"/> </FrameLayout> </FrameLayout>BaseRecyclerViewAdapterHelper的功能不仅仅于此,后续再继续补充。
转载于:https://www.cnblogs.com/it-tsz/p/11372187.html
相关资源:支持RecyclerView下拉刷新,上拉加载更多