给RecyclerView封装个Adapter吧(简单adapter)

单布局的Adapter:

  • 【给RecyclerView封装个Adapter吧(简单adapter)】首先明白Adapter需要什么数据,设计接口
    1. 数据
    2. 上下文
    3. 布局
      数据和上下文通常在构造方法里传入,所以要为布局设计个抽象方法:
      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; } });

效果:
给RecyclerView封装个Adapter吧(简单adapter)
文章图片
![Uploading Paste_Image_166316.png . . .]

    推荐阅读