让RecyclerView上拉刷新下拉加载更多更简便易用
<h3><strong>效果图</strong></h3> <p style="text-align:center"><img src="https://simg.open-open.com/show/c4cd91773a43642ad0bfa05fe8c19e32.gif"></p> <p style="text-align:center">dd.gif</p> <h3><strong>说明</strong></h3> <p>1.本demo使用的数据,均由gank.io提供</p> <p>2.下拉刷新使用的是SwipeRefreshLayout</p> <p>3.上拉加载更多使用的是BRVAH提供的BaseRecyclerViewAdapterHelper:2.1.3</p> <h3><strong>抽取接口</strong></h3> <pre> <code class="language-java">public interface IPagingService<T extends List> { /** * 加载分页数据 * @param page 加载第几页 * @param limit 1页加载多少条 */ void getData(int page,int limit, Observer<T> observer); }</code></pre> <h3><strong>编写基类(实现分页逻辑)</strong></h3> <pre> <code class="language-java">public class BasePagingActivity<T> extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener { private static final int PAGE_SIZE = 20; private RecyclerView mRecyclerView; private BaseQuickAdapter mQuickAdapter; private IPagingService<List<T>> mPagingService; private SwipeRefreshLayout mSwipeRefreshLayout; private int currentPage; private int lastPage; private void setSwipeRefreshLayout(SwipeRefreshLayout swipeRefreshLayout) { if (swipeRefreshLayout != null) { mSwipeRefreshLayout = swipeRefreshLayout; mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white); mSwipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light); swipeRefreshLayout.setOnRefreshListener(this); } else { throw new NullPointerException("swipeRefreshLayout not null"); } } private void setRecyclerView(RecyclerView recyclerView) { mRecyclerView = recyclerView; if (mRecyclerView.getLayoutManager() == null) { mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); } } private void setQuickAdapter(BaseQuickAdapter quickAdapter) { if (quickAdapter != null) { mQuickAdapter = quickAdapter; mQuickAdapter.openLoadAnimation(); mQuickAdapter.openLoadMore(PAGE_SIZE); mQuickAdapter.setOnLoadMoreListener(this); mRecyclerView.setAdapter(quickAdapter); } else { throw new NullPointerException("swipeRefreshLayout not null"); } } /** * 开始获取数据,提供给子类调用 */ protected void startGetData(RecyclerView recyclerView,SwipeRefreshLayout swipeRefreshLayout,BaseQuickAdapter quickAdapter, IPagingService<List<T>> pagingService){ mPagingService = pagingService; setRecyclerView(recyclerView); setSwipeRefreshLayout(swipeRefreshLayout); setQuickAdapter(quickAdapter); onLoadFirstData(); } @Override public void onRefresh() { currentPage = 1; mPagingService.getData(currentPage, PAGE_SIZE, new Observer<List<T>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Toast.makeText(BasePagingActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show(); mSwipeRefreshLayout.setRefreshing(false); currentPage = lastPage; } @Override public void onNext(List<T> list) { if (list == null) return; mQuickAdapter.getData().clear(); mQuickAdapter.addData(list); mQuickAdapter.notifyDataSetChanged(); mSwipeRefreshLayout.setRefreshing(false); } }); } @Override public void onLoadMoreRequested() { lastPage = currentPage; currentPage++; mPagingService.getData(currentPage, PAGE_SIZE, new Observer<List<T>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Toast.makeText(BasePagingActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show(); currentPage = lastPage; } @Override public void onNext(List<T> list) { if ((list != null && list.isEmpty())) { Toast.makeText(BasePagingActivity.this,"没有更多数据了",Toast.LENGTH_SHORT).show(); mQuickAdapter.addData(list); mQuickAdapter.loadComplete(); } else { mQuickAdapter.addData(list); } lastPage = currentPage; } }); } public void onLoadFirstData(){ lastPage = currentPage = 1; mSwipeRefreshLayout.setRefreshing(true); mPagingService.getData(currentPage, PAGE_SIZE, new Observer<List<T>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Toast.makeText(BasePagingActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show(); mSwipeRefreshLayout.setRefreshing(false); } @Override public void onNext(List<T> list) { if (list == null) return; mQuickAdapter.addData(list); mQuickAdapter.notifyDataSetChanged(); mSwipeRefreshLayout.setRefreshing(false); } }); } }</code></pre> <p>fragment同样可以这样做</p> <h3><strong>使用</strong></h3> <p>1.实现IPagingService</p> <pre> <code class="language-java">public class WelfareServer implements IPagingService<List<WelfareEntity>>{ @Override public void getData(int page, int limit, Observer<List<WelfareEntity>> observer) { RetrofitManager.getInstance().createReq(GankIo.class) .getWelfareImg(limit, page) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer); } }</code></pre> <h3>2.继承BasePagingActivity并调用startGetData方法</h3> <pre> <code class="language-java">public class MainActivity extends BasePagingActivity<WelfareEntity> { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout); RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.list); startGetData(mRecyclerView, mSwipeRefreshLayout, new BaseQuickAdapter<WelfareEntity>(R.layout.item_welfare,new ArrayList()){ @Override protected void convert(BaseViewHolder baseViewHolder, WelfareEntity welfareEntity) { Glide.with(MainActivity.this) .load(welfareEntity.getUrl()) .placeholder(R.mipmap.load_image_bg) .into((ImageView) baseViewHolder.getView(R.id.iv)); } },new WelfareServer()); } }</code></pre> <p> </p> <p> </p> <p>来自:http://www.jianshu.com/p/f3c3a84c2e99</p> <p> </p>
本文由用户 ElenaMacken 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!