BaseViewHolder.java
/** * 类说明:复用ViewHolder, 绑定 RecyclerView item 的控件 * Author: gaobaiq * Date: 2016/3/29 16:02 */ public class BaseViewHolder { private SparseArray<View> mViews; private View view; public static BaseViewHolder getViewHolder(View view) { BaseViewHolder baseViewHolder = (BaseViewHolder) view.getTag(); if (baseViewHolder == null) { baseViewHolder = new BaseViewHolder(view); view.setTag(baseViewHolder); } return baseViewHolder; } private BaseViewHolder(View view) { this.view = view; mViews = new SparseArray<>(); view.setTag(mViews); } public <T extends View> T get(int viewId) { View view = mViews.get(viewId); if (view == null) { view = this.view.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } public View getConvertView() { return view; } /** * 关于值的设置 */ public BaseViewHolder setText(int viewId, String text) { TextView tv = get(viewId); tv.setText(text); return this; } public BaseViewHolder setImageResource(int viewId, int resId) { ImageView view = get(viewId); view.setImageResource(resId); return this; } public BaseViewHolder setImageBitmap(int viewId, Bitmap bitmap) { ImageView view = get(viewId); view.setImageBitmap(bitmap); return this; } public BaseViewHolder setImageDrawable(int viewId, Drawable drawable) { ImageView view = get(viewId); view.setImageDrawable(drawable); return this; } public BaseViewHolder setBackgroundColor(int viewId, int color) { View view = get(viewId); view.setBackgroundColor(color); return this; } public BaseViewHolder setBackgroundRes(int viewId, int backgroundRes) { View view = get(viewId); view.setBackgroundResource(backgroundRes); return this; } public BaseViewHolder setTextColor(int viewId, int textColor) { TextView view = get(viewId); view.setTextColor(textColor); return this; } @SuppressLint("NewApi") public BaseViewHolder setAlpha(int viewId, float value) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { get(viewId).setAlpha(value); } else { // Pre-honeycomb hack to set Alpha value AlphaAnimation alpha = new AlphaAnimation(value, value); alpha.setDuration(0); alpha.setFillAfter(true); get(viewId).startAnimation(alpha); } return this; } public BaseViewHolder setVisible(int viewId, boolean visible) { View view = get(viewId); view.setVisibility(visible ? View.VISIBLE : View.GONE); return this; } public BaseViewHolder setVisible(int viewId, int visible) { View view = get(viewId); view.setVisibility(visible); return this; } public BaseViewHolder linkify(int viewId) { TextView view = get(viewId); Linkify.addLinks(view, Linkify.ALL); return this; } public BaseViewHolder setTypeface(Typeface typeface, int... viewIds) { for (int viewId : viewIds) { TextView view = get(viewId); view.setTypeface(typeface); view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); } return this; } public BaseViewHolder setProgress(int viewId, int progress) { ProgressBar view = get(viewId); view.setProgress(progress); return this; } public BaseViewHolder setProgress(int viewId, int progress, int max) { ProgressBar view = get(viewId); view.setMax(max); view.setProgress(progress); return this; } public BaseViewHolder setMax(int viewId, int max) { ProgressBar view = get(viewId); view.setMax(max); return this; } public BaseViewHolder setRating(int viewId, float rating) { RatingBar view = get(viewId); view.setRating(rating); return this; } public BaseViewHolder setRating(int viewId, float rating, int max) { RatingBar view = get(viewId); view.setMax(max); view.setRating(rating); return this; } public BaseViewHolder setTag(int viewId, Object tag) { View view = get(viewId); view.setTag(tag); return this; } public BaseViewHolder setTag(int viewId, int key, Object tag) { View view = get(viewId); view.setTag(key, tag); return this; } public BaseViewHolder setChecked(int viewId, boolean checked) { Checkable view = get(viewId); view.setChecked(checked); return this; } }
RecyclerViewHolder.java
/** * 类说明: * Author: gaobaiq * Date: 2016/3/29 16:20 */ public class RecyclerViewHolder extends RecyclerView.ViewHolder { private BaseViewHolder baseViewHolder; public RecyclerViewHolder(View itemView) { super(itemView); baseViewHolder = BaseViewHolder.getViewHolder(itemView); } public BaseViewHolder getViewHolder() { return baseViewHolder; } }
CommonRecyclerAdapter.java
/** * 类说明:RecyclerView 适配器基类 * Author: gaobaiq * Date: 2016/3/29 15:58 */ public abstract class CommonRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> { protected Context mContext; protected List<T> mList; protected ViewHolderClick<T> holderClick; protected AdapterView.OnItemClickListener onItemClickListener; protected AdapterView.OnItemLongClickListener onItemLongClickListener; public CommonRecyclerAdapter(Context context) { this.mContext = context; this.mList = new ArrayList<>(); } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(onCreateViewLayoutID(viewType), parent, false); return new RecyclerViewHolder(view); } @Override public void onViewRecycled(final RecyclerViewHolder holder) { super.onViewRecycled(holder); } @Override public void onBindViewHolder(final RecyclerViewHolder holder, final int position) { onBindViewHolder(holder.getViewHolder(), position); if (onItemClickListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.onItemClick(null, v, holder.getAdapterPosition(), holder.getItemId()); } }); } if (onItemLongClickListener != null) { holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { onItemLongClickListener.onItemLongClick(null, v, holder.getAdapterPosition(), holder.getItemId()); return false; } }); } } public abstract int onCreateViewLayoutID(int viewType); public abstract void onBindViewHolder(BaseViewHolder holder, int position); @Override public int getItemCount() { return mList == null ? 0 : mList.size(); } /** * 获取列表 */ public List<T> getList() { return mList; } /** * 替换某一个元素 */ public void replaceBean(int position, T t) { if (t != null) { this.mList.remove(position); this.mList.add(position, t); notifyItemChanged(position, t); } } /** * 添加数据列表到列表头部 */ public void addListAtStart(List<T> list) { if (list != null && !list.isEmpty()) { this.mList.addAll(0, list); notifyDataSetChanged(); } } /** * 添加数据列表到列表尾部 */ public void addListAtEnd(List<T> list) { if (list != null && !list.isEmpty()) { this.mList.addAll(list); notifyItemRangeInserted(mList.size() - 1, list.size()); } } /** * 添加数据列表到列表尾部 */ public void addListAtEndAndNotify(List<T> list) { if (list != null && !list.isEmpty()) { this.mList.addAll(list); notifyDataSetChanged(); } } /** * 添加单个元素到列表头 */ public void addListBeanAtStart(T t) { if (t != null) { mList.add(0, t); notifyItemInserted(0); } } /** * 添加单个元素到列表尾 */ public void addListBeanAtEnd(T t) { if (t != null) { mList.add(t); notifyItemInserted(mList.size() - 1); } } /** * 替换RecyclerView数据 */ public void replaceList(List<T> list) { if (list != null){ this.mList = list; } else { mList.clear(); } notifyDataSetChanged(); } /** * 替换RecyclerView中的某一个数据 */ public void replaceItem(T t, int position) { if (position >= 0 && position <= mList.size() && t != null) { this.mList.set(position, t); notifyItemChanged(position); } } /** * 删除RecyclerView所有数据 */ public void removeAll() { if (mList != null) { notifyItemRangeRemoved(0, mList.size()); this.mList.clear(); } } /** * 删除RecyclerView指定位置的数据 */ public void remove(T t) { if(mList != null) { this.mList.remove(t); notifyDataSetChanged(); } } /** * 删除RecyclerView指定位置的数据 */ public void remove(int position) { if (position >= 0 && position <= mList.size() && mList != null) { this.mList.remove(position); notifyItemRemoved(position); } } /** * item点击事件抽象方法 */ public AdapterView.OnItemClickListener getOnItemClickListener() { return onItemClickListener; } public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } /** * item长按事件抽象方法 */ public AdapterView.OnItemLongClickListener getOnItemLongClickListener() { return onItemLongClickListener; } public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener onItemLongClickListener) { this.onItemLongClickListener = onItemLongClickListener; } /** * 点击事件抽象方法 */ public void setOnHolderClick(ViewHolderClick<T> holderClick) { this.holderClick = holderClick; } public interface ViewHolderClick<T> { void onViewClick(View view, T t, int position); } }
调用方法:
注意:DeviceBean是一个对象,你可以传任何对象或者String,int,long等,调用的时候只需要mAdapter.replaceList(List<DeviceBean>);即可。
/** * 类说明:我的设备 * Author: gaobaiq * Date: 2016/8/12 10:27 */ public class MyDeviceAdapter extends CommonRecyclerAdapter<DeviceBean> { public MyDeviceAdapter(Context context) { super(context); } @Override public int onCreateViewLayoutID(int viewType) { return R.layout.item_of_my_device; } @Override public void onBindViewHolder(BaseViewHolder holder, int position) { DeviceBean bean = mList.get(position); //TextView tvTitle = holder.get(R.id.tv_title); //tvTitle.setText(bean.getTitle()); holder.setText(R.id.tv_title, bean.getTitle()); } }
时间: 2024-11-15 03:50:53