1. 程式人生 > >RecyclerView上拉載入更多下拉重新整理優雅姿勢

RecyclerView上拉載入更多下拉重新整理優雅姿勢

BaseRecyclerViewAdapterHelper+EasyRefreshLayout優雅實現recyclerView上拉載入更多下拉重新整理
直接上程式碼:
佈局:
<com.ajguan.library.EasyRefreshLayout
android:id="@+id/easylayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
  <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerList"
android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/transparent"> </android.support.v7.widget.RecyclerView> </com.ajguan.library.EasyRefreshLayout>
配置程式碼:
mManager = new GridLayoutManager(this.getContext(), 6);
easyRefreshLayout.setRefreshHeadView(new 
RefreshHeaderView(getActivity())); mRecyclerList.setLayoutManager(mManager); //設定adapter productAdapter = new ProductAdapter(this.getContext(), R.layout.item_product, new ArrayList()); productAdapter.openLoadAnimation(); mRecyclerList.setHasFixedSize(true); mRecyclerList.setAdapter(productAdapter);

監聽事件:
productAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
  @Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
    MyUtil.outLog("item" + position);
}
});
easyRefreshLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
  @Override
public void onLoadMore() {
}

@Override
public void onRefreshing() {
}})
介面卡:
public class ProductAdapter extends BaseQuickAdapter<ResultBean, BaseViewHolder> {
  private Context context;
  public ProductAdapter(Context context, int layoutResId, List data) {
    super(layoutResId, data);
    this.context = context;
}

  @Override
protected void convert(BaseViewHolder helper, ResultBean item) {
    helper.setText(R.id.tv_product_price, "¥" + item.getOfferPrice());
//    helper.setImageResource(R.id.icon,R.mipmap.icon_price_background );
//狀態處理注意helper複用問題    Glide.with(context).load(item.getImageUrl()).diskCacheStrategy(DiskCacheStrategy.ALL).override(200, 500)
      .fitCenter().into((ImageView) helper.getView(R.id.iv_product_pic));
}
}
具體使用請參考官方文件:
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'//是一個強大的RecyclerAdapter框架優點不多說:https://www.jianshu.com/p/b343fcff51b0
compile 'com.github.anzaizai:EasyRefreshLayout:1.3.1'//這是一個獨立的上拉載入下拉重新整理庫