单布局的Adapter:
-
首先明白Adapter需要什么数据,设计接口
- 数据
- 上下文
- 布局
数据和上下文通常在构造方法里传入,所以要为布局设计个抽象方法:
abstract public int getItemLayoutId(int viewType);
声明上下文和数据:
protected final List<T> mData; protected final Context mContext; protected LayoutInflater mInflater;
构造方法:
protected BaseRecyclerAdapter(List<T> data, Context context) { mData = data; mContext = context; mInflater = LayoutInflater.from(context); }
添加常用方法。。。
完整代码:
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> { protected final List<T> mData; protected final Context mContext; protected LayoutInflater mInflater; protected BaseRecyclerAdapter(List<T> data, Context context) { mData = 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<View> mViews;//集合类,layout里包含的View,以view的id作为key,value是view对象 private Context mContext;//上下文对象
构造方法里赋值:
super(itemView); mContext = context; mViews = new SparseArray<View>();
返回View:
private <T extends View> 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<String>(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; } });
效果: