前言
一般我们要在mainActivity中利用RecycleView展示一个列表数据的时候,adapter都是类似以下写法:
public class NameAdapter<T> extends RecyclerView.Adapter {
protected Context mContext;
protected View mLayoutView;
protected List<T> mData;
private ViewHolder mViewHolder;
public NameAdapter(Context context, List<T> data) {
this.mContext = context;
this.mData = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mLayoutView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false);
mViewHolder = new ViewHolder(mLayoutView);
return mViewHolder;
}
@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mViewHolder = ((ViewHolder) holder);
initData(position);
setListener(position);
}
private void initData(int position){
mViewHolder.mTvName.setText(mData.get(position).toString());
}
private void setListener(int position){
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView mTvName;
public ViewHolder(View view) {
super(view);
mTvName = (TextView) view.findViewById(R.id.tv_name);
}
}
}
然后在mainActivity中调用如下:
mNameList= Arrays.asList("小明","小红","小芳","小花","小海","小林","小叶","小虎","小柔");
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mNameAdapter = new NameAdapter<>(this, mNameList);
mRecyclerView.setAdapter(mNameAdapter);
那么有什么方法能缩减NameAdapter中的代码,使我们在使用RecycleView进行列表数据展示的时候,把NameAdapter也的更简洁些呢,下面就给大家介绍一款adapter框架,相信在使用它后,将大大缩减NameAdapter的代码
1.配置
在projected的build.gradle中配置maven:
maven { url "https://jitpack.io" }
具体配置如下:
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
在app module的build.gradle中添加依赖:
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'
自此,配置结束.
2.写一个公共的adapter,名为ComAdapter,集成基本的初始化,加载动画等方法
public abstract class ComAdapter<T> extends BaseQuickAdapter<T, BaseViewHolder> {
protected Context mContext;
protected List<T>mData;
protected int mItemViewId;//item布局id
protected View mHeaderView;
protected View mFooterView;
public ComAdapter(int itemViewId,List<T>data,Context context) {
super(itemViewId, data);
this.mItemViewId=itemViewId;
this.mContext=context;
this.mData=data;
}
@Override
protected void convert(BaseViewHolder viewHolder, T t) {
initView(viewHolder,t);
initData(viewHolder,t);
setListener(viewHolder,t);
}
//以下代码见例子
//......
3.自己写的NameAdapter继承于ComAdapter
public class NameAdapter<T>extends ComAdapter {
private TextView mTvName;
public NameAdapter(List<T> data, Context context){
super(R.layout.item_layout,data,context);
}
@Override
protected void initView(BaseViewHolder viewHolder, Object o) {
mTvName=viewHolder.getView(R.id.tv_name);
}
//以下代码省略
//......
4.在mainActivity中的使用
声明
@BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
private List<String>mNameList;
private NameAdapter mNameAdapter;
初始化
mNameList= Arrays.asList("小明","小红","小芳","小花","小海","小林","小叶","小虎","小柔");
mNameAdapter=new NameAdapter(mNameList,mContext);//初始化NameAdapter
mNameAdapter.setRecyclerManager(mRecyclerView);//设置RecyclerView特性
mNameAdapter.addHeaderView(R.layout.item_header);//添加header
mNameAdapter.addFooterView(R.layout.item_footer);//添加footer
mNameAdapter.openLeftAnimation();//设置加载动画
item点击事件
//item點擊事件
mNameAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "onItemLongClick" + position, Toast.LENGTH_SHORT).show();
}
});
item中子控件点击事件
//子空控件点击事件
mNameAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "onItemLongClick" + mNameList.get(position), Toast.LENGTH_SHORT).show();
}
});
5.效果图
6.項目結構圖
一个RecycleView的强大adapter
注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权