这次的主题是封装RecyclerView的单选多选,现在大家应该都是用的RecyclerView开发列表数据吧。网上也有很多RecyclerView的封装库,各种眼花缭乱的功能,这里分享一个我自己项目里用的简易版RecyclerView 的轻便版封装吧。
其实这篇博客是我后面一篇博客的准备~一句话实现RecyclerView的单选多选的选项列表
先看图,这些都是RrcyclerView做的各种样式的列表数据视图~
多选样式图:
multichoice.jpg1、封装通用ViewHolder
public static class VH extends RecyclerView.ViewHolder {
private SparseArray<View> mViews;
private View mConvertView;
private VH(View v) {
super(v);
mConvertView = v;
mViews = new SparseArray<>();
}
public static VH get(ViewGroup parent, int layoutId) {
View convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
return new VH(convertView);
}
public <T extends View> T getView(int id) {
View v = mViews.get(id);
if (v == null) {
v = mConvertView.findViewById(id);
mViews.put(id, v);
}
return (T) v;
}
public void setText(int id, String value) {
TextView view = getView(id);
view.setText(value);
}
}
原理就是通过SparseArray<View>存储item view的控件(不知道SpareseArray的请自行google这个数据结构的Api),getView(int id)的功能就是通过id获得对应的View:因为findViewById比较耗费性能,首先在mViews中查询是否存在,如果没有,那么findViewById()并放入mViews中,避免下次再执行findViewById()。
2、封装RecyclerView的通用快速开发adapter
public abstract class AbsRecycleAdapter<T> extends RecyclerView.Adapter<AbsRecycleAdapter.VH> {
private OnItemClickListener mItemClickListener;
private List<T> mData;
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
final VH viewHolder = VH.get(parent, getLayoutId(viewType));
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = viewHolder.getAdapterPosition();
if (mItemClickListener != null) {
mItemClickListener.onItemClick(position, v);
}
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(VH holder, int position) {
convert(holder, mData.get(position), position);
}
public abstract int getLayoutId(int viewType);
public abstract void convert(VH holder, T data, int position);
public void setOnItemClickListener(OnItemClickListener listener) {
this.mItemClickListener = listener;
}
public void setData(List<T> beans) {
if (null == mData) {
mData = new ArrayList<>();
}
mData.clear();
if (ValidateUtil.isValidate(beans)) {
mData.addAll(beans);
}
}
public void addData(List<T> beans) {
if (null == mData) {
setData(beans);
} else {
mData.addAll(beans);
}
}
public void addData(T t) {
if (mData == null) {
mData = new ArrayList<>();
}
if (!mData.contains(t)) {
mData.add(t);
}
}
public void remove(T t) {
if (null != mData) {
mData.remove(t);
}
}
public List<T> getData() {
return mData;
}
public void clear() {
if (mData != null) {
mData.clear();
}
}
@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
}
public T getItem(int position) {
if (mData != null) {
return mData.get(position);
}
return null;
}
public interface OnItemClickListener {
void onItemClick(int position, View v);
}
public static class VH extends RecyclerView.ViewHolder {
private SparseArray<View> mViews;
private View mConvertView;
private VH(View v) {
super(v);
mConvertView = v;
mViews = new SparseArray<>();
}
public static VH get(ViewGroup parent, int layoutId) {
View convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
return new VH(convertView);
}
public <T extends View> T getView(int id) {
View v = mViews.get(id);
if (v == null) {
v = mConvertView.findViewById(id);
mViews.put(id, v);
}
return (T) v;
}
public void setText(int id, String value) {
TextView view = getView(id);
view.setText(value);
}
public void setTextColor(int id, @ColorInt int color) {
TextView view = getView(id);
view.setTextColor(color);
}
public void setTextBackgroundResource(int id, @DrawableRes int resid) {
TextView view = getView(id);
view.setBackgroundResource(resid);
}
public void setBackgroundResource(int id, @DrawableRes int resId) {
View view = getView(id);
view.setBackgroundResource(resId);
}
public void setViewShow(int id, boolean isShow) {
View view = getView(id);
view.setVisibility(isShow ? View.VISIBLE : View.GONE);
}
public void setImageUrl(int id, String imageUrl) {
ImageView imageView = getView(id);
Glide.with(itemView.getContext())
.load(imageUrl)
.placeholder(R.mipmap.banner)
.error(R.mipmap.banner)
.into(imageView);
}
public void setImageResid(int id, int resourceId) {
ImageView imageView = getView(id);
imageView.setImageResource(resourceId);
}
public void setImageCircleAvatarUrl(int id, String imageUrl) {
ImageView imageView = getView(id);
Glide.with(itemView.getContext())
.load(imageUrl)
.placeholder(R.mipmap.avatar_default)
.error(R.mipmap.avatar_default)
.bitmapTransform(new GlideCircleTransform(itemView.getContext()))
.into(imageView);
}
}
}
网友评论