RecyclerView续,下拉刷新与上拉加载
wholesale010
8年前
<p>上次写了RecyclerView的强大,忘了补充好多个问题,今天全部补上。</p> <p>首先下拉刷新,这个简单,所以我也只是简单说说</p> <p>我一直都是认为谷歌有自带的框架就不用第三方,所以当然是用SwipeRefreshLayout来下拉刷新</p> <p>配置下很简单</p> <pre> <code class="language-java">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();//获取数据 } };</code></pre> <p>官方控件简简单单的下拉刷新就完成了</p> <p>接下来是上拉加载,别人扔给我的控件</p> <pre> <code class="language-java">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(); } };</code></pre> <p>那个设置状态的几种类型:</p> <pre> <code class="language-java">RecyclerViewStateUtils.setFooterViewState(mRecyclerView, LoadingFooter.State.Normal); //加载完成后不显示的正常状态 RecyclerViewStateUtils.setFooterViewState(mRecyclerView, LoadingFooter.State.TheEnd);//没有下一页的状态 RecyclerViewStateUtils.setFooterViewState(Activity.this, mRecyclerView, 10, LoadingFooter.State.Loading, null);//加载状态</code></pre> <p>接着上EndlessRecyclerOnScrollListener</p> <pre> <code class="language-java">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); } } } }</code></pre> <p>好了上拉加载完成了</p> <p>建议:加载的时候可以清楚上拉加载的监听,完成了,确定不是最后一页了再加上</p> <p>mRecyclerView.clearOnScrollListeners();<br> mRecyclerView.addOnScrollListener(mOnScrollListener);</p> <p> </p> <p>来自:http://www.jianshu.com/p/7fde3ec5a23e</p> <p> </p>