项目中使用的通用Adapter 和ViewHolder ,这两个只支持单一ItemType
CommonAdapter
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
protected Context mContext;
protected int mLayoutId;
protected List<T> mDatas;
protected LayoutInflater mInflater;
protected ViewGroup mRv;
private OnItemClickListener mOnItemClickListener;
public CommonAdapter setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
return this;
}
public interface OnItemClickListener<T> {
void onItemClick(ViewGroup parent, View view, T t, int position);
boolean onItemLongClick(ViewGroup parent, View view, T t, int position);
}
public OnItemClickListener getmOnItemClickListener() {
return mOnItemClickListener;
}
public CommonAdapter(Context context, int layoutId, List<T> datas) {
mContext = context;
mInflater = LayoutInflater.from(context);
mLayoutId = layoutId;
mDatas = datas;
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
ViewHolder viewHolder = ViewHolder.get(mContext, null, parent, mLayoutId, -1);
if (null == mRv) {
mRv = parent;
}
return viewHolder;
}
protected int getPosition(RecyclerView.ViewHolder viewHolder) {
return viewHolder.getAdapterPosition();
}
protected boolean isEnabled(int viewType) {
return true;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.updatePosition(position);
setListener(position, holder);
convert(holder, mDatas.get(position),position);
}
protected void setListener(final int position, final ViewHolder viewHolder) {
if (!isEnabled(getItemViewType(position))) return;
viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(mRv, v, mDatas.get(position), position);
}
}
});
viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mOnItemClickListener != null) {
int position = getPosition(viewHolder);
return mOnItemClickListener.onItemLongClick(mRv, v, mDatas.get(position), position);
}
return false;
}
});
}
public abstract void convert(ViewHolder holder, T t,int position);
@Override
public int getItemCount() {
return mDatas != null ? mDatas.size() : 0;
}
/**
* 刷新数据,初始化数据
*
* @param list
*/
public void setDatas(List<T> list) {
if (this.mDatas != null) {
if (null != list) {
List<T> temp = new ArrayList<>();
temp.addAll(list);
this.mDatas.clear();
this.mDatas.addAll(temp);
} else {
this.mDatas.clear();
}
} else {
this.mDatas = list;
}
notifyDataSetChanged();
}
/**
* 删除数据
*
* @param i
*/
public void remove(int i) {
if (null != mDatas && mDatas.size() > i && i > -1) {
mDatas.remove(i);
notifyDataSetChanged();
}
}
/**
* 加载更多数据
*
* @param list
*/
public void addDatas(List<T> list) {
if (list.size() > 0 && null != list) {
List<T> temp = new ArrayList<>();
temp.addAll(list);
if (this.mDatas != null) {
this.mDatas.addAll(temp);
} else {
this.mDatas = temp;
}
notifyDataSetChanged();
}
}
public List<T> getDatas() {
return mDatas;
}
public T getItem(int position) {
if (position > -1 && null != mDatas && mDatas.size() > position) {
return mDatas.get(position);
}
return null;
}
}
ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> mViews;
private int mPosition;
private View mConvertView;
protected Context mContext;
private int mLayoutId;
public ViewHolder(Context context, View itemView, ViewGroup parent, int position) {
super(itemView);
mContext = context;
mConvertView = itemView;
mPosition = position;
mViews = new SparseArray<View>();
mConvertView.setTag(this);
// itemView.setBackgroundDrawable(itemView.getResources().getDrawable(R.drawable.app_listview_item_bg));
}
public ViewHolder(Context context, View itemView) {
super(itemView);
mContext = context;
mConvertView = itemView;
mViews = new SparseArray<View>();
}
public static ViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
View itemView = LayoutInflater.from(context).inflate(layoutId, parent,
false);
ViewHolder holder = new ViewHolder(context, itemView, parent, position);
holder.mLayoutId = layoutId;
return holder;
} else {
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.mPosition = position;
return holder;
}
}
/**
* 通过viewId获取控件
*
* @param viewId
* @return
*/
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public View getConvertView() {
return mConvertView;
}
/**
* 设置TextView的值
*
* @param viewId
* @param text
* @return
*/
public ViewHolder setText(int viewId, CharSequence text) {
TextView tv = getView(viewId);
tv.setText(text);
return this;
}
public ViewHolder setSelected(int viewId, boolean selected) {
View v = getView(viewId);
v.setSelected(selected);
return this;
}
public ViewHolder setImageResource(int viewId, int resId) {
ImageView view = getView(viewId);
view.setImageResource(resId);
return this;
}
public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
ImageView view = getView(viewId);
view.setImageBitmap(bitmap);
return this;
}
public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
ImageView view = getView(viewId);
view.setImageDrawable(drawable);
return this;
}
public ViewHolder setBackgroundColor(int viewId, int color) {
View view = getView(viewId);
view.setBackgroundColor(color);
return this;
}
public ViewHolder setBackgroundRes(int viewId, int backgroundRes) {
View view = getView(viewId);
view.setBackgroundResource(backgroundRes);
return this;
}
public ViewHolder setTextColor(int viewId, int textColor) {
TextView view = getView(viewId);
view.setTextColor(textColor);
return this;
}
public ViewHolder setTextColorRes(int viewId, int textColorRes) {
TextView view = getView(viewId);
view.setTextColor(mContext.getResources().getColor(textColorRes));
return this;
}
@SuppressLint("NewApi")
public ViewHolder setAlpha(int viewId, float value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getView(viewId).setAlpha(value);
} else {
// Pre-honeycomb hack to set Alpha value
AlphaAnimation alpha = new AlphaAnimation(value, value);
alpha.setDuration(0);
alpha.setFillAfter(true);
getView(viewId).startAnimation(alpha);
}
return this;
}
public ViewHolder setVisible(int viewId, boolean visible) {
View view = getView(viewId);
view.setVisibility(visible ? View.VISIBLE : View.GONE);
return this;
}
public ViewHolder linkify(int viewId) {
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
public ViewHolder setTypeface(Typeface typeface, int... viewIds) {
for (int viewId : viewIds) {
TextView view = getView(viewId);
view.setTypeface(typeface);
view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
return this;
}
public ViewHolder setProgress(int viewId, int progress) {
ProgressBar view = getView(viewId);
view.setProgress(progress);
return this;
}
public ViewHolder setProgress(int viewId, int progress, int max) {
ProgressBar view = getView(viewId);
view.setMax(max);
view.setProgress(progress);
return this;
}
public ViewHolder setMax(int viewId, int max) {
ProgressBar view = getView(viewId);
view.setMax(max);
return this;
}
public ViewHolder setRating(int viewId, float rating) {
RatingBar view = getView(viewId);
view.setRating(rating);
return this;
}
public ViewHolder setRating(int viewId, float rating, int max) {
RatingBar view = getView(viewId);
view.setMax(max);
view.setRating(rating);
return this;
}
public ViewHolder setTag(int viewId, Object tag) {
View view = getView(viewId);
view.setTag(tag);
return this;
}
public ViewHolder setTag(int viewId, int key, Object tag) {
View view = getView(viewId);
view.setTag(key, tag);
return this;
}
public ViewHolder setChecked(int viewId, boolean checked) {
Checkable view = (Checkable) getView(viewId);
view.setChecked(checked);
return this;
}
/**
* 关于事件的
*/
public ViewHolder setOnClickListener(int viewId,
View.OnClickListener listener) {
View view = getView(viewId);
view.setOnClickListener(listener);
return this;
}
public ViewHolder setOnTouchListener(int viewId,
View.OnTouchListener listener) {
View view = getView(viewId);
view.setOnTouchListener(listener);
return this;
}
public ViewHolder setOnLongClickListener(int viewId,
View.OnLongClickListener listener) {
View view = getView(viewId);
view.setOnLongClickListener(listener);
return this;
}
public void updatePosition(int position) {
mPosition = position;
}
public int getLayoutId() {
return mLayoutId;
}
/**
* 隐藏或展示Item
*
* @param visible
*/
public void setItemVisible(boolean visible) {
View v = getConvertView();
if (null != v) {
if (visible) {
if (null != v.getLayoutParams()) {
v.getLayoutParams().width = AbsListView.LayoutParams.MATCH_PARENT;
v.getLayoutParams().height = AbsListView.LayoutParams.WRAP_CONTENT;
} else {
v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
}
v.setVisibility(View.VISIBLE);
} else {
if (null != v.getLayoutParams()) {
v.getLayoutParams().width = -1;
v.getLayoutParams().height = 1;
} else {
v.setLayoutParams(new AbsListView.LayoutParams(-1, 1));
}
v.setVisibility(View.GONE);
}
}
}
public void setHItemVisible(boolean visible) {
View v = getConvertView();
if (null != v) {
if (visible) {
if (null != v.getLayoutParams()) {
v.getLayoutParams().width = AbsListView.LayoutParams.WRAP_CONTENT;
v.getLayoutParams().height = AbsListView.LayoutParams.WRAP_CONTENT;
} else {
v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
}
v.setVisibility(View.VISIBLE);
} else {
if (null != v.getLayoutParams()) {
v.getLayoutParams().width = -1;
v.getLayoutParams().height = 1;
} else {
v.setLayoutParams(new AbsListView.LayoutParams(-1, 1));
}
v.setVisibility(View.GONE);
}
}
}
}
使用
mRecyclerView.setAdapter(mAdapter = new CommonAdapter<CartoonMoiveItemBean>(getActivity(), R.layout.cartton_item, data) {
@Override
public void convert(ViewHolder holder, CartoonMoiveItemBean cartoonMoiveItemBean, int position) {
NoNullUtils.setText((TextView) holder.getView(R.id.movie_name), cartoonMoiveItemBean.getTitle());
// NoNullUtils.setText((TextView) holder.getView(R.id.movie_title), cartoonMoiveItemBean.getTitle());
SimpleDraweeView draweeView = holder.getView(R.id.cartoon_simpleview);
draweeView.setAspectRatio(1.75f);
draweeView.setImageURI(cartoonMoiveItemBean.getThumbnail_url());
}
});
mAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener<CartoonMoiveItemBean>() {
@Override
public void onItemClick(ViewGroup parent, View view, CartoonMoiveItemBean cartoonMoiveItemBean, int position) {
JumpUtils.toCartoomWebViewFragment(getActivity(),cartoonMoiveItemBean.getUrl());
}
@Override
public boolean onItemLongClick(ViewGroup parent, View view, CartoonMoiveItemBean cartoonMoiveItemBean, int position) {
return false;
}
});