SmartRefreshLayout刷新组件github地址
1、BaseApplication中做全局配置
// TODO: 全局一次性配置下拉刷新组件默认属性 static { //启用矢量图兼容 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); //设置全局默认配置(优先级最低,会被其他设置覆盖) SmartRefreshLayout.setDefaultRefreshInitializer(new DefaultRefreshInitializer() { @Override public void initialize(@NonNull Context context, @NonNull RefreshLayout layout) { layout.setEnableRefresh(true); layout.setEnableLoadMore(false); layout.setEnableAutoLoadMore(false); layout.setEnableOverScrollBounce(false); layout.setDisableContentWhenRefresh(true); layout.setDisableContentWhenLoading(true); layout.setFooterHeight(45); } }); //设置全局默认的Header SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() { @NonNull @Override public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) { layout.setPrimaryColorsId(R.color.kidney_head, R.color.kidney_hint); return new ClassicsHeader(context).setTextSizeTitle(13).setTextSizeTime(10) .setTimeFormat(new DynamicTimeFormat("更新于:%s")) .setArrowResource(R.drawable.ic_arrow_refresh); } }); //设置Header文本 ClassicsHeader.REFRESH_HEADER_PULLING = "下拉"; ClassicsHeader.REFRESH_HEADER_REFRESHING = "加载中..."; ClassicsHeader.REFRESH_HEADER_LOADING = "加载中..."; ClassicsHeader.REFRESH_HEADER_RELEASE = "释放刷新"; ClassicsHeader.REFRESH_HEADER_FINISH = "刷新成功"; ClassicsHeader.REFRESH_HEADER_FAILED = "刷新失败"; //设置全局默认的Footer SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() { @NonNull @Override public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) { return new ClassicsFooter(context).setPrimaryColorId(R.color.kidney_head).setTextSizeTitle(13) .setArrowResource(R.drawable.ic_arrow_refresh); } }); //设置Footer文本 ClassicsFooter.REFRESH_FOOTER_PULLING = "上拉加载更多"; ClassicsFooter.REFRESH_FOOTER_RELEASE = "释放立即加载"; ClassicsFooter.REFRESH_FOOTER_REFRESHING = "...正在加载..."; ClassicsFooter.REFRESH_FOOTER_LOADING = "...正在加载..."; ClassicsFooter.REFRESH_FOOTER_FINISH = "加载完成"; ClassicsFooter.REFRESH_FOOTER_FAILED = "加载失败"; ClassicsFooter.REFRESH_FOOTER_NOTHING = "已经到底了"; }2、在SmartRefreshLayout的基础上,加一些自己项目的统一处理
public class KSmartRefreshLayout extends SmartRefreshLayout { private int mPageSize = 10; public KSmartRefreshLayout(Context context) { super(context); } public KSmartRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); } public KSmartRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 设置每次加载的数量 */ public void setPageSize(int pageSize) { this.mPageSize = pageSize; } /** * 完成下拉刷新 * 恢复没有更多数据的原始状态 */ public void refreshComplete() { finishRefresh(); resetNoMoreData(); } /** * 完成上拉加载更多 * * @param resultSize 当前List.Size * @param time pageIndex */ public void setResultSize(int resultSize, int time) { if (resultSize == 0) { if (time == 0) { //展示空View } finishLoadMoreWithNoMoreData(); } else if (resultSize > 0 && resultSize < mPageSize) { finishLoadMoreWithNoMoreData(); } else { finishLoadMore(); } } }3、XML中的使用
<com.syg.android.lib.view.recycleView.KSmartRefreshLayout android:id="@+id/refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent" app:srlEnableAutoLoadMore="true" app:srlEnableLoadMore="true"> <android.support.v7.widget.RecyclerView android:id="@+id/recycle_view" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never"/> </com.syg.android.lib.view.recycleView.KSmartRefreshLayout>4、Activity中的使用
refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() { @Override public void onLoadMore(@NonNull RefreshLayout refreshLayout) { getGetDoctorLists(); } @Override public void onRefresh(@NonNull RefreshLayout refreshLayout) { } }); refreshLayout.autoRefresh(); if (state == HttpMsg.OK) { List<Doctor> temp = (List<Doctor>) data; if (pageIndex == 0) { resultList.clear(); } resultList.addAll(temp); adapter.notifyDataSetChanged(); pageIndex++; refreshLayout.setResultSize(temp.size(), pageIndex); } else { refreshLayout.finishLoadMore(false); } refreshLayout.refreshComplete(); }5、关于RecycleView的适配,用的是BRVAH的万能适配器