BaseQuickAdapter——RecyclerView通用适配器
RecyclerView是Android L版本中新添加的一个用于取代ListView的SDK,具有灵活性和可替代性比ListView更好,RecyclerView同样也用到适配,作为开发者,我们希望有一款通用的适配。
BaseQuickAdapter 就是一款为Android开发者打造的针对繁琐的适配器的构建的快速开源项目。
BaseQuickAdapter 特点
可以减少重复代码
添加Item点击事件,长按事件以及子控件的点击事件
添加头部、尾部、下拉刷新、上拉加载涵盖多种动画处理,没有更多数据处理
添加Item滑动动画。多种动画切换
添加新增删除Item动画
网格、列表、流式布局随意切换
添加空布局(列表无数据时,显示更加友好)
拖拽和侧滑删除
支持多类型布局
字母导航
BaseQuickAdapter 使用
导入依赖
由于BaseQuickAdapter 各种版本比较多,此处我们使用 BaseRecyclerViewAdapterHelper
先在 build.gradle(Project:XXXX) 的 repositories 添加:
allprojects { repositories { ... maven { url"https://jitpack.io"} } }
然后在 build.gradle(Module:app) 的 dependencies 添加:
dependencies { compile'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'}
# gradle//最新代码还没更新到 jcenter 仓库,推荐使用方法二依赖项目compile'com.github.baserecycleradapter:library:1.1.0'# 下载源码依赖
使用
首先需要继承BaseQuickAdapter 然后 BaseQuickAdapter 第一个泛型Status是数据实体类型,第二个BaseViewHolder是ViewHolder其目的是为了支持扩展ViewHolder
mRecyclerView.setAdapter(mAdapter =newBaseQuickAdapter(R.layout.rv_item, getItemDatas()) {@Overrideprotectedvoidconvert(finalBaseViewHolder helper, String item){ helper.setText(R.id.tv_item_text, item); }});
Item点击 长按事件
//Item 点击事件setOnItemClickListener(intviewId, AdapterView.OnItemClickListener listener)//长按事件setOnItemLongClickListener(intviewId, AdapterView.OnItemLongClickListener listener)
子控件的点击事件
helper.setOnClickListener(R.id.tv_item_text,newView.OnClickListener() {@OverridepublicvoidonClick(View v){//事件处理} });
添加头部、尾部
//头部mAdapter.addHeaderView(); View headerView=getLayoutInflater().inflate(R.layout.rv_header,null); headerView.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)); mAdapter.addHeaderView(headerView);//尾部mAdapter.addFooterView();
添加Item动画
//一行代码开启动画 默认CUSTOM动画mAdapter.openLoadAnimation(BaseQuickAdapter.CUSTOMIN);# 更换动画//ALPHAIN, SCALEIN, SLIDEIN_BOTTOM, SLIDEIN_LEFT, SLIDEIN_RIGHT, //SLIDEIN_LEFT_RIGHT, SLIDEIN_BOTTOM_TOP, CUSTOMINmAdapter.openLoadAnimation(BaseQuickAdapter.CUSTOMIN);//自定义mAdapter.openLoadAnimation(newBaseAnimation[]{newBaseAnimation() { @OverridepublicAnimator[] getAnimators(View view) {returnnewAnimator[]{ObjectAnimator.ofFloat(view,"alpha",0.5f,1.0f), ObjectAnimator.ofFloat(view,"scaleX",0.5f,1.0f)}; } }});
设置加载更多
mAdapter.setOnLoadMoreListener(newBaseQuickAdapter.RequestLoadMoreListener() {@OverridepublicvoidonLoadMoreRequested(){ } });
加载更多 加载失败 状态
mShowType++;if(mShowType ==2) { mHandler.postDelayed(newRunnable() {@Overridepublicvoidrun(){//加载失败,点击重试mAdapter.loadMoreFail(); } }, DELAY_MILLIS); }elseif(mShowType >=4) { mHandler.post(newRunnable() {@Overridepublicvoidrun(){//加载完成mAdapter.loadMoreEnd(); } }); }else{ mHandler.postDelayed(newRunnable() {@Overridepublicvoidrun(){ mAdapter.addData(addDatas());//加载更多完成mAdapter.loadMoreComplete(); } }, DELAY_MILLIS); }
流式布局的简单实现
mRecyclerView.setLayoutManager(newFlowLayoutManager());
多布局
package com.example.administrator.gaoqizhong.ui.adapter;import android.widget.ImageView;import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;import com.chad.library.adapter.base.BaseViewHolder;import com.example.administrator.gaoqizhong.R;import com.example.administrator.gaoqizhong.data.beans.Info;import com.squareup.picasso.Picasso;import java.util.List;/** * Created by Administrator on 2018/6/12 0012. * * 继承自万能适配器 BaseMultiItemQuickAdapter , 需要实体类实现 MultiItemEntity 接口 * */public class MyMultiAdapter extends BaseMultiItemQuickAdapter{ private Listlist; public MyMultiAdapter(List data) {
super(data);
addItemType(0, R.layout.layout_item);
addItemType(1, R.layout.layout_item2);
addItemType(2, R.layout.layout_item3);
this.list = data;
}
@Override
public int getItemViewType(int position) {
int itemType = 0;
if(null == list.get(position).getThumbnail_pic_s02() && null == list.get(position).getThumbnail_pic_s03()){
itemType = 0;
}else if(null == list.get(position).getThumbnail_pic_s03()){
itemType = 1;
}else{
itemType = 2;
}
return itemType;
}
@Override
protected void convert(BaseViewHolder helper, Info.ResultBean.DataBean item) {
switch (helper.getItemViewType()){
case 0:
helper.setText(R.id.author_name,item.getTitle());
Picasso.with(mContext).load(item.getThumbnail_pic_s()).into((ImageView) helper.getView(R.id.img));
break;
case 1:
helper.setText(R.id.author_name,item.getTitle());
Picasso.with(mContext).load(item.getThumbnail_pic_s()).into((ImageView) helper.getView(R.id.img));
Picasso.with(mContext).load(item.getThumbnail_pic_s02()).into((ImageView) helper.getView(R.id.img2));
break;
case 2:
helper.setText(R.id.author_name,item.getTitle());
Picasso.with(mContext).load(item.getThumbnail_pic_s()).into((ImageView) helper.getView(R.id.img));
Picasso.with(mContext).load(item.getThumbnail_pic_s02()).into((ImageView) helper.getView(R.id.img2));
Picasso.with(mContext).load(item.getThumbnail_pic_s03()).into((ImageView) helper.getView(R.id.img3));
break;
}
}
}