这次对RecylcerView的adapter经行封装,以减少重复代码的调用。
实现的功能:
- 返回统一数据的点击事件
- 多条目布局
- 统一的图片加载
- 极度简洁的调用流程
普通使用:
adapter代码只需要
public class MyTaskAdapter extends BaseAdapter<MyTaskModel> {
public MyTaskAdapter(Context context) {
super(context, R.layout.item_mytask_fragment);
}
@Override
protected void bindData(ViewHolder holder, MyTaskModel item) {
holder.setText(R.id.task_end_tv,item.getLateEndTime());
holder.setText(R.id.task_start_tv,item.getAssignTime());
holder.setText(R.id.task_no_tv,item.getTaskId());
holder.setText(R.id.task_title_tv,item.getTaskName());
holder.setText(R.id.task_state_tv,item.getTaskStatus()+"");
}
}
多条目布局
mRecyclerView = findViewById(R.id.recycler);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
TalkAdapter mTalkAdapter=new TalkAdapter(this, new OnItemViewSupport<TalkBean>() {
@Override
public int getSupportItemView(TalkBean item) {
if(item.getId()==1){
return R.layout.item_me;
}else if(item.getId()==2){
return R.layout.item_friends;
}
return 0;
}
});
mRecyclerView.setAdapter(mTalkAdapter);
mTalkAdapter.setOnItemClickListener(new OnItemClickListener<TalkBean>() {
@Override
public void onItemClick(TalkBean item, int position) {
}
});
public class TalkAdapter extends BaseAdapter<TalkBean> {
public TalkAdapter(Context context, OnItemViewSupport<TalkBean> itemSupport) {
super(context, itemSupport);
}
@Override
protected void bindData(ViewHolder holder, TalkBean item) {
if(item.getId()==1){
holder.setText(R.id.me,item.getText());
}else if(item.getId()==2){
holder.setText(R.id.frident,item.getText());
}
}
}
设置图片,可以直接设置资源文件或者网络图片
@Override
protected void bindData(ViewHolder holder, TestBean item) {
holder.setText(R.id.me,item.getText());
//网络图片
holder.setImage(R.id.image, new MyImageLoader(item.getPath()));
//资源文件
holder.setImageResource(R.id.image,R.mipmap.ic_launcher);
}
这里需要建一个类MyImageLoader,可以使用任意加载图片的框架
public class MyImageLoader extends ViewHolder.ImageLoader {
public MyImageLoader(String path) {
super(path);
}
@Override
protected void loadImage(ImageView imageView, String path) {
Glide.with(context).load(path)
.error(R.mipmap.ic_no_data)
.into(imageView);
}
}
是不是非常简洁。
下面封装的代码如下
BaseAdapter
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
private Context mContext;
private List<T> mDats=new ArrayList<>();
private int mLayoutId;
private LayoutInflater mInflater;
private OnItemViewSupport mItemSupport;
private OnItemClickListener<T> clickListener;
//正常布局
public BaseAdapter(Context context, int layoutId){
mContext=context;
mLayoutId= layoutId;
mInflater = LayoutInflater.from(context);
}
public void addDatas(List<T> datas){
mDats.addAll(datas);
}
public void setDatas(List<T> datas){
mDats=datas;
}
//多布局调用这个
public BaseAdapter(Context context, OnItemViewSupport<T> itemSupport){
this(context ,0);
mItemSupport=itemSupport;
}
//dia
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(mItemSupport!=null){
mLayoutId=viewType;
}
return new ViewHolder(mInflater.inflate(mLayoutId,parent,false));
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final T item = mDats.get(position);
bindData(holder, item);
if(clickListener!=null){
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onItemClick(item,position);
}
});
}
}
protected abstract void bindData(ViewHolder holder, T item);
@Override
public int getItemCount() {
return mDats.size();
}
@Override
public int getItemViewType(int position) {
//处理多布局
if (mItemSupport!=null){
return mItemSupport.getSupportItemView(mDats.get(position));
}
return super.getItemViewType(position);
}
public void setOnItemClickListener(OnItemClickListener<T> clickListener){
this.clickListener = clickListener;
}
}
条目点击的监听事件
public interface OnItemClickListener<T> {
void onItemClick(T item, int position);
}
多条目支持的接口
public interface OnItemViewSupport<T> {
int getSupportItemView(T item);
}
ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> mViews;
public ViewHolder(View itemView) {
super(itemView);
mViews=new SparseArray();
}
/**
* 获取View方法
* @param id
* @param <T>
* @return
*/
public <T extends View> T getView(@IdRes int id){
View view= mViews.get(id);
if(view==null){
view =itemView.findViewById(id);
mViews.put(id,view);
}
return (T) view;
}
/**
* 设置文本
*/
public ViewHolder setText(@IdRes int id, CharSequence text){
TextView textView = getView(id);
if(TextUtils.isEmpty(text)){
textView.setText("");
}else{
textView.setText(text);
}
return this;
}
/**
* 设置图片
*/
public ViewHolder setImageResource(@IdRes int id, @DrawableRes int resId){
ImageView view = getView(id);
view.setImageResource(resId);
return this;
}
/**
* 设置网络图片
*/
public ViewHolder setImage(@IdRes int id, ImageLoader loader){
ImageView view = getView(id);
loader.loadImage(view,loader.mPath);
return this;
}
/**
* 解耦图片处理
*/
public abstract static class ImageLoader{
String mPath;
public ImageLoader(String path){
this.mPath=path;
}
protected abstract void loadImage(ImageView imageView, String path);
}
}