给RecyclerView封装个Adapter吧(简单adapter)
单布局的Adapter:
- 【给RecyclerView封装个Adapter吧(简单adapter)】首先明白Adapter需要什么数据,设计接口
- 数据
- 上下文
- 布局
数据和上下文通常在构造方法里传入,所以要为布局设计个抽象方法:
abstract public int getItemLayoutId(int viewType);
声明上下文和数据:
protected final List
mData; protected final Context mContext; protected LayoutInflater mInflater;
构造方法:
protected BaseRecyclerAdapter(List
data, Context context) { mData = https://www.it610.com/article/data; mContext = context; mInflater = LayoutInflater.from(context); }
添加常用方法。。。
完整代码:
public abstract class BaseRecyclerAdapter
extends RecyclerView.Adapter { protected final List mData; protected final Context mContext; protected LayoutInflater mInflater; protected BaseRecyclerAdapter(List data, Context context) { mData = https://www.it610.com/article/data; mContext = context; mInflater = LayoutInflater.from(context); } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { final RecyclerViewHolder holder = new RecyclerViewHolder(mContext,mInflater.inflate(getItemLayoutId(viewType), parent, false)); return holder; } @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { bindData(holder, position, mData.get(position)); } @Override public int getItemCount() { return mData.size(); } public void add(int pos, T item) { mData.add(pos, item); notifyItemInserted(pos); } public void delete(int pos) { mData.remove(pos); notifyItemRemoved(pos); } abstract public void bindData(RecyclerViewHolder holder, int position, T item); abstract public int getItemLayoutId(int viewType); }
- 下面编写ViewHolder
成员变量:
private SparseArray
mViews; //集合类,layout里包含的View,以view的id作为key,value是view对象 private Context mContext; //上下文对象
构造方法里赋值:
super(itemView); mContext = context; mViews = new SparseArray
();
返回View:
private
T findViewById(int viewId) { View view = mViews.get(viewId); if (view == null) { view = itemView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; }
添加常用方法:
public View getView(int viewId) { return findViewById(viewId); } public TextView getTextView(int viewId) { return (TextView) getView(viewId); } public Button getButton(int viewId) { return (Button) getView(viewId); } public ImageView getImageView(int viewId) { return (ImageView) getView(viewId); } public ImageButton getImageButton(int viewId) { return (ImageButton) getView(viewId); } public EditText getEditText(int viewId) { return (EditText) getView(viewId); } public RecyclerViewHolder setText(int viewId, String value) { TextView view = findViewById(viewId); view.setText(value); return this; } public RecyclerViewHolder setBackground(int viewId, int resId) { View view = findViewById(viewId) ; view.setBackgroundResource(resId); return this; }
一个初级的Adapter成型了。
-使用
recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new BaseRecyclerAdapter(datas,this) { @Override public void bindData(RecyclerViewHolder holder, int position, String item) { holder.setText(R.id.tvItemName,item); }@Override public int getItemLayoutId(int viewType) { return R.layout.item_adapter; } });
文章图片
![Uploading Paste_Image_166316.png . . .]
推荐阅读
- 竞相开花的金融科技何时结果(CB-Insights给出了这样一份报告)
- 爱情|爱情 | 给她
- 松松写给我的信
- 投稿|戴森“新宠”,败给了7元的硅胶扫把?
- 目前,医院在发展的过程中遇到人才的问题,优秀人才的稀缺导致医院缺乏良好的人才团队阶梯,在这个问题上,请给于一些建议!
- 1986年的春晚0点报时,给人的感觉很奇怪()
- 投稿|再给周杰伦一首歌的时间
- 给大家分享一个鉴别厨艺的方法
- 航拍高手、吉他十级,6500+Star|航拍高手、吉他十级,6500+Star 开源项目作者,后浪程序员给力!
- python基础|[python基础] 面向对象——封装,继承