唉,好久好久没写文章了,简直就是罪过,工作忙不是借口,自己懒才是真的,本来中间还有一篇唠叨的,内容也没啥,就是工作上忙呗,不想加班呗,还有累呗,写了,但没发,算了········
正事,上次写了RecyclerView的强大,忘了补充好多个问题,今天全部补上。
首先下拉刷新,这个简单,所以我也只是简单说说
我一直都是认为谷歌有自带的框架就不用第三方,所以当然是用SwipeRefreshLayout来下拉刷新
配置下很简单
mSwipeRefreshLayout.setOnRefreshListener(mOnRefreshListener);
mSwipeRefreshLayout.setColorSchemeResources(R.color.status_bar_bg, R.color.orange, R.color.green);
/**
* 刷新
*/
private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
pageNum = 1;
mAdapter.clearList();//清楚数据,上一篇文章的事
getData();//获取数据
}
};
官方控件简简单单的下拉刷新就完成了
接下来是上拉加载,别人扔给我的控件
mRecyclerView.addOnScrollListener(mOnScrollListener);
/**
* RecycleView的滑动监听(加载更多)
*/
private EndlessRecyclerOnScrollListener mOnScrollListener = new EndlessRecyclerOnScrollListener() {
@Override
public void onLoadNextPage(View view) {
super.onLoadNextPage(view);
//下面这是重点,第三个参数是一页10条(用来控制是否可以继续下拉),第四个是显示加载状态,最后一个可以配置点击重试请求网络
RecyclerViewStateUtils.setFooterViewState(Activity.this, mRecyclerView, 10, LoadingFooter.State.Loading, null);
pageNum++;
getData();
}
};
那个设置状态的几种类型:
RecyclerViewStateUtils.setFooterViewState(mRecyclerView, LoadingFooter.State.Normal); //加载完成后不显示的正常状态
RecyclerViewStateUtils.setFooterViewState(mRecyclerView, LoadingFooter.State.TheEnd);//没有下一页的状态
RecyclerViewStateUtils.setFooterViewState(Activity.this, mRecyclerView, 10, LoadingFooter.State.Loading, null);//加载状态
接着上EndlessRecyclerOnScrollListener
public class RecyclerViewStateUtils {
/**
* 设置headerAndFooterAdapter的FooterView State
*
* @param instance context
* @param recyclerView recyclerView
* @param pageSize 分页展示时,recyclerView每一页的数量
* @param state FooterView State
* @param errorListener FooterView处于Error状态时的点击事件
*/
public static void setFooterViewState(Activity instance, RecyclerView recyclerView, int pageSize, LoadingFooter.State state, View.OnClickListener errorListener) {
if(instance==null || instance.isFinishing()) {
return;
}
RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
if (outerAdapter == null || !(outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter)) {
return;
}
HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter = (HeaderAndFooterRecyclerViewAdapter) outerAdapter;
//只有一页的时候,就别加什么FooterView了
if (headerAndFooterAdapter.getInnerAdapter().getItemCount() < pageSize) {
return;
}
LoadingFooter footerView;
//已经有footerView了
if (headerAndFooterAdapter.getFooterViewsCount() > 0) {
footerView = (LoadingFooter) headerAndFooterAdapter.getFooterView();
footerView.setState(state);
if (state == LoadingFooter.State.NetWorkError) {
footerView.setOnClickListener(errorListener);
}
recyclerView.scrollToPosition(headerAndFooterAdapter.getItemCount() - 1);
} else {
footerView = new LoadingFooter(instance);
footerView.setState(state);
if (state == LoadingFooter.State.NetWorkError) {
footerView.setOnClickListener(errorListener);
}
headerAndFooterAdapter.addFooterView(footerView);
recyclerView.scrollToPosition(headerAndFooterAdapter.getItemCount() - 1);
}
}
/**
* 获取当前RecyclerView.FooterView的状态
*
* @param recyclerView
*/
public static LoadingFooter.State getFooterViewState(RecyclerView recyclerView) {
RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
if (((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterViewsCount() > 0) {
LoadingFooter footerView = (LoadingFooter) ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterView();
return footerView.getState();
}
}
return LoadingFooter.State.Normal;
}
/**
* 设置当前RecyclerView.FooterView的状态
*
* @param recyclerView
* @param state
*/
public static void setFooterViewState(RecyclerView recyclerView, LoadingFooter.State state) {
RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
if (((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterViewsCount() > 0) {
LoadingFooter footerView = (LoadingFooter) ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterView();
footerView.setState(state);
}
}
}
}
好了上拉加载完成了
建议:加载的时候可以清楚上拉加载的监听,完成了,确定不是最后一页了再加上
mRecyclerView.clearOnScrollListeners();
mRecyclerView.addOnScrollListener(mOnScrollListener);
唉,当初刚出校园,满腔热血,现在想买辆单车都买不起,怪不得从古代起就辣么多怀才不遇的诗写出来,宝宝还是努力干活吧······