RecycleView 어댑터 패키지 사용 (2)
/**
* Adapter
*/
public class BaseAdapterRecycler extends RecyclerView.Adapter {
protected Context mContext;
//.
protected List mDatas;
//.
private OnItemClickListener mOnItemClickListener;
//. SparseArrayCompat
private SparseArrayCompat> mTemplates;
/**
*
*
* @param context
*/
public BaseAdapterRecycler(Context context) {
this.mContext = context;
mTemplates = new SparseArrayCompat<>();
}
/**
*
*
* @param context
* @param datas
*/
public BaseAdapterRecycler(Context context, List datas) {
this(context);
this.setmDatas(datas);
}
/**
*
*
* @param datas
*/
public void setmDatas(List datas) {
this.mDatas = datas;
notifyDataSetChanged();
}
/**
*
*
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
//
if (isOutOfBounds(position)) {
return super.getItemViewType(position);
} else {
//
for (int i = mTemplates.size() - 1; i >= 0; i--) {
ItemTemplate itemTemplate = mTemplates.valueAt(i);
if (itemTemplate.isForViewType(mDatas.get(position))) {
// , onCreateViewHolder()
// , addItemTemplate()
return mTemplates.keyAt(i);
}
}
return super.getItemViewType(position);
}
}
/**
* ViewHolder
*
* @param viewGroup
* @param viewType
* @return
*/
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
//
ItemTemplate itemTemplate = mTemplates.get(viewType);
// ViewHolder
ViewHolder holder = ViewHolder.createViewHolder(mContext, viewGroup, itemTemplate.getLayoutID());
//
setListener(viewGroup, holder, viewType);
return holder;
}
/**
* ViewHolder
*
* @param viewHolder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
DATA data = mDatas.get(position);
for (int i = 0; i < mTemplates.size(); i++) {
ItemTemplate template = mTemplates.valueAt(i);
if (template.isForViewType(data)) {
template.convert(viewHolder, data, position);
}
}
}
/**
* GridLayoutManager ,
* recyclerView GridLayoutManager ,SpanCount
* :
* A:2
* B:3
* C:2
* D:1
* SpanCount 6
* Bean
* A:3(6/2)
* B:2(6/3)
* C:3(6/2)
* D:6(6/1)
*
* RecyclerView ,
* Item
*
* @param recyclerView
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
if (lm instanceof GridLayoutManager) {
((GridLayoutManager) lm).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
DATA data = mDatas.get(position);
for (int i = 0; i < mTemplates.size(); i++) {
ItemTemplate template = mTemplates.valueAt(i);
if (template.isForViewType(data)) {
return template.getSpanCount();
}
}
return 1;
}
});
}
}
/**
*
*
* @return
*/
@Override
public int getItemCount() {
return mDatas == null ? 0 : mDatas.size();
}
/**
*
*
* @return
*/
public List getDatas() {
return mDatas;
}
/**
*
*
* @param data
* @return
*/
public BaseAdapterRecycler addData(DATA data) {
if (mDatas == null) {
mDatas = new ArrayList<>();
}
if (data != null) {
mDatas.add(data);
} else {
log("data is null");
}
notifyDataSetChanged();
return this;
}
/**
*
*
* @param datas
* @return
*/
public BaseAdapterRecycler addData(List datas) {
if (mDatas == null) {
mDatas = new ArrayList<>();
}
if (datas != null) {
mDatas.addAll(datas);
} else {
log("data is null");
}
notifyDataSetChanged();
return this;
}
/**
*
*
* @param position
* @param data
* @return
*/
public BaseAdapterRecycler addData(@IntRange(from = 0) int position, DATA data) {
if (mDatas == null) {
mDatas = new ArrayList<>();
}
if (isOutOfBounds(position)) {
log("addData() position , ");
position = getItemCount();
}
if (data != null) {
mDatas.add(position, data);
} else {
log("data is null");
}
notifyDataSetChanged();
return this;
}
/**
*
*
* @param position
* @param datas
* @return
*/
public BaseAdapterRecycler addData(@IntRange(from = 0) int position, List datas) {
if (mDatas == null) {
mDatas = new ArrayList<>();
}
if (isOutOfBounds(position)) {
log("addData() position , ");
position = getItemCount();
}
if (datas != null) {
mDatas.addAll(position, datas);
} else {
log("datas is null");
}
notifyDataSetChanged();
return this;
}
/**
* position
*
* @param position
* @return
*/
public BaseAdapterRecycler deleteData(@IntRange(from = 0) int position) {
return removeData(position);
}
/**
* position
*
* @param position
* @return
*/
public BaseAdapterRecycler removeData(@IntRange(from = 0) int position) {
if (mDatas == null) {
mDatas = new ArrayList<>();
}
if (isOutOfBounds(position)) {
log("removeData() position ");
} else {
mDatas.remove(position);
}
notifyDataSetChanged();
return this;
}
/**
*
*
* (holder.itemView.setOnclickListener(this))
*
* @param viewGroup
* @param holder
* @param viewType
*/
private void setListener(ViewGroup viewGroup, ViewHolder holder, int viewType) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, holder, holder.getAdapterPosition());
}
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemLongClick(v, holder, holder.getAdapterPosition());
}
return false;
}
});
}
/**
*
*
* @param onItemClickListener
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
/**
* position
*
* @param position
* @return
*/
private boolean isOutOfBounds(int position) {
if (mDatas == null || mDatas.isEmpty()) {
return true;
} else if (position < 0 || position >= mDatas.size()) {
return true;
} else {
return false;
}
}
/**
*
*
* @param itemTemplate
* @return
*/
public BaseAdapterRecycler addItemTemplate(ItemTemplate itemTemplate) {
if (itemTemplate != null) {
mTemplates.put(mTemplates.size(), itemTemplate);
} else {
log("itemTemplate is null");
}
return this;
}
/**
*
*
* @param viewType
* @param itemTemplate
* @return
*/
public BaseAdapterRecycler addItemTemplate(int viewType, ItemTemplate itemTemplate) {
if (mTemplates.get(viewType) != null) {
log(" " + viewType + " ," + " " + mTemplates.get(viewType));
}
if (itemTemplate != null) {
log(" " + mTemplates.get(viewType) + " ");
mTemplates.put(viewType, itemTemplate);
}
return this;
}
/**
*
*/
public interface OnItemClickListener {
void onItemClick(View view, RecyclerView.ViewHolder holder, int position);
boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position);
}
/**
*
*
* @param log
*/
private void log(String log) {
Log.e(getClass().getName(), log);
}
/**
* SparseArray(API >19 ) SparseArrayCompat( )
* android
* HashMap
*/
/*
put(int key, E value)
append(int key, E value)
public E get(int key)
public E get(int key, E valueIfKeyNotFound)
//
// index
public int keyAt(int index)
// index
public E valueAt(int index)
// <0( -1)
public int indexOfKey(int key)
// -1
public int indexOfValue(E value)
public void delete(int key)
public void remove(int key)
public void removeAt(int index)
public void clear()
public void setValueAt(int index, E value)
public void put(int key, E value)
*/
}
복합 기 ViewHolder 를 사용 할 때 도 이러한 것 은 수정 할 필요 가 없습니다. 항목 에 플러그 인 이나 jar 패키지 (Glide...) 가 추가 되 어 있 으 면 적 절 히 수정 할 수 있 습 니 다.
/**
*
*/
public class ViewHolder extends RecyclerView.ViewHolder {
private Context mContext;
private View mConvertView;
private SparseArray mViews;
public ViewHolder(Context context, @NonNull View itemView) {
super(itemView);
mContext = context;
mConvertView = itemView;
mViews = new SparseArray();
}
/**
* ViewHolder
*
* @param context
* @param itemView
* @return
*/
public static ViewHolder createViewHolder(Context context, View itemView) {
return new ViewHolder(context, itemView);
}
/**
* ViewHolder
*
* @param context
* @param parent
* @param layoutRes
* @return
*/
public static ViewHolder createViewHolder(Context context, ViewGroup parent, @LayoutRes int layoutRes) {
return new ViewHolder(context, LayoutInflater.from(context).inflate(layoutRes, null));
}
/**
*
*
* @param idRes
* @return
*/
public WeakReference getWeakReference(@IdRes int idRes) {
if (mViews.get(idRes) == null) {
mViews.put(idRes, itemView.findViewById(idRes));
}
return new WeakReference<>(mViews.get(idRes));
}
/**
* idRes
*
* @param idRes
* @param
* @return
*/
public V getView(@IdRes int idRes) {
if (mViews.get(idRes) == null) {
mViews.put(idRes, itemView.findViewById(idRes));
}
if (mViews.get(idRes) == null) {
throw new NullPointerException(idRes + " ");
}
return (V) mViews.get(idRes);
}
///******************** *****************************************///
/**
* TextView
*
* @param idRes
* @param sequence
* @return
*/
public ViewHolder setText(@IdRes int idRes, CharSequence sequence) {
TextView tv = getView(idRes);
if (tv == null) {
return this;
}
if (sequence == null || sequence.length() == 0) {
tv.setVisibility(View.INVISIBLE);
} else {
tv.setText(sequence);
}
return this;
}
/**
* TextView
*
* @param idRes
* @param color
* @return
*/
public ViewHolder setTextColor(@IdRes int idRes, @ColorInt int color) {
TextView tv = getView(idRes);
tv.setTextColor(color);
return this;
}
/**
* TextView
*
* @param idRes
* @param sequence
* @param color
* @return
*/
public ViewHolder setText(@IdRes int idRes, CharSequence sequence, @ColorInt int color) {
TextView tv = getView(idRes);
tv.setTextColor(color);
setText(idRes, sequence);
return this;
}
/**
* Resources TextView
*
* @param idRes
* @param stringRes
* @return
*/
public ViewHolder setText(@IdRes int idRes, @StringRes int stringRes) {
return setText(idRes, mContext.getResources().getString(stringRes));
}
/**
* TextView
*
* @param idRes
* @param stringRes
* @param color
* @return
*/
public ViewHolder setText(@IdRes int idRes, @StringRes int stringRes, @ColorInt int color) {
return setText(idRes, mContext.getResources().getString(stringRes), color);
}
public @interface LinkifyMask {
}
/**
* TextView
*
* @param idRes
* @param linkify
* @return
*/
public ViewHolder setTextLinkify(@IdRes int idRes, @LinkifyMask int linkify) {
// Linkify.WEB_URLS
// Linkify.EMAIL_ADDRESSES
// Linkify.PHONE_NUMBERS
// Linkify.MAP_ADDRESSES map
// Linkify.ALL
TextView tv = getView(idRes);
Linkify.addLinks(tv, linkify);
return this;
}
/**
* ImageView
*
* @param idRes
* @param colorRes
* @return
*/
public ViewHolder setImageColor(@IdRes int idRes, @ColorRes int colorRes) {
return setImageColor(idRes, colorRes, PorterDuff.Mode.MULTIPLY);
}
/**
* ImageView
*
* @param idRes
* @param colorRes
* @param mode
* @return
*/
public ViewHolder setImageColor(@IdRes int idRes, @ColorRes int colorRes, PorterDuff.Mode mode) {
ImageView iv = getView(idRes);
iv.setColorFilter(colorRes, mode);
// PorterDuff.Mode.CLEAR;// 0
// PorterDuff.Mode.SRC;//
// PorterDuff.Mode.DST;//
// PorterDuff.Mode.SRC_OVER;//
// PorterDuff.Mode.DST_OVER;//
// PorterDuff.Mode.SRC_IN;// ,
// PorterDuff.Mode.DST_IN;// ,
// PorterDuff.Mode.SRC_OUT;//
// PorterDuff. Mode.DST_OUT;//
// PorterDuff.Mode.SRC_ATOP;// ,
// PorterDuff.Mode.DST_ATOP;// ,
// PorterDuff.Mode.XOR;// 0,
// PorterDuff.Mode.ADD;
// PorterDuff.Mode.MULTIPLY;// , 255
// PorterDuff.Mode.SCREEN;// : , 。
// PorterDuff.Mode.OVERLAY;// , 。
// PorterDuff.Mode.DARKEN;// ,
// PorterDuff.Mode.LIGHTEN;// ,
return this;
}
/**
* ImageView
*
* @param idRes
* @return
*/
public ViewHolder clearImageColor(@IdRes int idRes) {
ImageView iv = getView(idRes);
iv.clearColorFilter();
return this;
}
/**
* ImageView
*
* @param idRes
* @param resId
* @return
*/
public ViewHolder setImageResources(@IdRes int idRes, @DrawableRes int resId) {
ImageView iv = getView(idRes);
iv.setImageResource(resId);
return this;
}
/**
*
*
* @param idRes
* @param color
* @return
*/
public ViewHolder setBackgroundColor(@IdRes int idRes, @ColorInt int color) {
getView(idRes).setBackgroundColor(color);
return this;
}
/**
*
*
* @param idRes
* @param resId
* @return
*/
public ViewHolder setBackgroundColorRes(@IdRes int idRes, @ColorRes int resId) {
setBackgroundColor(idRes, mContext.getResources().getColor(resId));
return this;
}
/**
*
*
* @param idRes
* @param resId
* @return
*/
public ViewHolder setBackgroundRes(@IdRes int idRes, @DrawableRes int resId) {
getView(idRes).setBackgroundResource(resId);
return this;
}
/**
*
*
* @param idRes
* @return
*/
public ViewHolder setGone(@IdRes int idRes) {
getView(idRes).setVisibility(View.GONE);
return this;
}
/**
*
*
* @param idRes
* @return
*/
public ViewHolder setVisible(@IdRes int idRes) {
getView(idRes).setVisibility(View.VISIBLE);
return this;
}
/**
*
*
* @param idRes
* @return
*/
public ViewHolder setInVisible(@IdRes int idRes) {
getView(idRes).setVisibility(View.INVISIBLE);
return this;
}
/**
* ProgressBar
*
* @param idRes
* @param progress
* @return
*/
public ViewHolder setProgress(@IdRes int idRes, int progress) {
ProgressBar bar = getView(idRes);
bar.setProgress(progress);
return this;
}
/**
* ProgressBar
*
* @param idRes
* @param progress
* @param max
* @return
*/
public ViewHolder setProgress(@IdRes int idRes, int progress, int max) {
ProgressBar bar = getView(idRes);
bar.setProgress(progress);
bar.setMax(max);
return this;
}
/**
* ProgressBar
*
* @param idRes
* @param max
* @return
*/
public ViewHolder setProgressMax(@IdRes int idRes, int max) {
ProgressBar bar = getView(idRes);
bar.setMax(max);
return this;
}
/**
* Tag
*
* @param idRes
* @param tag
* @return
*/
public ViewHolder setTag(@IdRes int idRes, Object tag) {
getView(idRes).setTag(tag);
return this;
}
/**
* Tag
*
* @param idRes
* @param key
* @param tag
* @return
*/
public ViewHolder setTag(@IdRes int idRes, @StringRes int key, Object tag) {
getView(idRes).setTag(key, tag);
return this;
}
/**
* /
*
* @param idRes
* @param checked
* @return
*/
public ViewHolder setChecked(@IdRes int idRes, boolean checked) {
Checkable checkable = getView(idRes);
checkable.setChecked(checked);
return this;
}
/**
*
*
* @param idRes
* @param listener
* @return
*/
public ViewHolder setOnClickListener(@IdRes int idRes, View.OnClickListener listener) {
getView(idRes).setOnClickListener(listener);
return this;
}
/**
*
*
* @param idRes
* @param listener
* @return
*/
public ViewHolder setOnTouchListener(@IdRes int idRes, View.OnTouchListener listener) {
getView(idRes).setOnTouchListener(listener);
return this;
}
/**
*
*
* @param idRes
* @param listener
* @return
*/
public ViewHolder setOnLongClickListener(@IdRes int idRes, View.OnLongClickListener listener) {
getView(idRes).setOnLongClickListener(listener);
return this;
}
/**
*
*
* @param idRes
* @param listener
* @return
*/
public ViewHolder setOnListener(@IdRes int idRes, CompoundButton.OnCheckedChangeListener listener) {
CheckBox cb = getView(idRes);
cb.setOnCheckedChangeListener(listener);
return this;
}
/**
* Tag
*
* @param idRes
* @return
*/
public Tag getTag(@IdRes int idRes) {
return (Tag) getView(idRes).getTag();
}
/**
* Tag
*
* @param idRes
* @param key
* @return
*/
public Tag getTag(@IdRes int idRes, @StringRes int key) {
return (Tag) getView(idRes).getTag(key);
}
}
레이아웃 모드 인터페이스
public interface ItemTemplate {
/**
*
*
* @return
*/
int getLayoutID();
/**
* item
*
* @param data
* @return
*/
boolean isForViewType(DATA data);
/**
* GridLayoutManager ,
*
* :
* A:2
* B:3
* C:2
* D:1
* SpanCount 6
* Bean
* A:3(6/2)
* B:2(6/3)
* C:3(6/2)
* D:6(6/1)
*
* RecyclerView ,
* Item
* JAVA 8
* @return
*/
default int getSpanCount(){
return 1;
}
/**
*
* @param holder
* @param data
* @param position
*/
void convert(ViewHolder holder, DATA data, int position);
}
특히 주의
getSpanCount()
이 방법 은 RecyclerView 에서 GridLayoutManager 로 레이아웃 을 설정 할 때 만 호출 됩 니 다. 모든 레이아웃 모드 의 열 수가 같 을 때 이 방법 을 실현 할 수 있 습 니 다. 다른 상황 은 이 방법 을 복사 해 야 합 니 다.쓰다
public class Template implements ItemTemplate {
/**
*
*
* @return
*/
@Override
public int getLayoutID() {
return R.layout.layout_1;
}
/**
*
*
* @param baseBean
* @return
*/
@Override
public boolean isForViewType(String baseBean) {
return true;
}
/**
*
*
* @param holder
* @param baseBean
* @param position
*/
@Override
public void convert(ViewHolder holder, String baseBean, int position) {
holder.setText(R.id.tv, baseBean);
}
}
xml 는 보 내지 않 습 니 다. 모두 가장 간단 합 니 다.