Android RecyclerView 通用介面卡的實現
分析
- 我們需要在RecyclerViewAdapter內部維持一個資料集合的引用
- 需要實現Item的點選事件,包括短按和長按,並需要向外部提供設定監聽器的方法
- 這個介面卡必須通用,所以應該是泛型的,並且ViewHolder也是通用的,可以採用SparseArray維持View的所有集合
- 在RecyclerViewAdapter內部需要實現資料的繫結(抽象方法,延遲到子類實現),監聽器的繫結
編寫監聽器
分為短按事件和長按事件
短按監聽器
package cn.edu.zafu.base.adapter.listener; import android.view.View; /** * Created by lizhangqu on 2015/6/3. */ public interface OnItemClickListener<T> { void onClick(View view, T item); }
長按監聽器
package cn.edu.zafu.base.adapter.listener; import android.view.View; /** * Created by lizhangqu on 2015/6/3. */ public interface OnItemLongClickListener<T> { void onLongClick(View view, T item); }
編寫通用介面卡
設定監聽器
package cn.edu.zafu.base.adapter; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.List; import cn.edu.zafu.base.adapter.listener.OnItemClickListener; import cn.edu.zafu.base.adapter.listener.OnItemLongClickListener; /** * this is a BaseAdapter for RecyclerView * author lizhangqu * version 1.0 * date 2015/6/3. */ public abstract class BaseRecyclerViewAdapter<T, VH extends BaseRecyclerViewAdapter.SparseArrayViewHolder> extends RecyclerView.Adapter<VH> { /** * click listener */ protected OnItemClickListener mOnItemClickListener; /** * long click listener */ protected OnItemLongClickListener mOnItemLongClickListener; /** * set a long click listener * * @param onItemLongClickListener */ public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) { mOnItemLongClickListener = onItemLongClickListener; } /** * set a click listener * * @param onItemClickListener */ public void setOnItemClickListener(OnItemClickListener onItemClickListener) { mOnItemClickListener = onItemClickListener; } }
編寫構造方法,將資料集合進行賦值
/** * data */ protected List<T> mList; /** * @param list the datas to attach the adapter */ public BaseRecyclerViewAdapter(List<T> list) { mList = list; }
實現幾個必要的方法,比如獲得Item以及覆寫getItemCount方法
/** * get a item by index * * @param position * @return */ protected T getItem(int position) { return mList.get(position); } @Override public int getItemCount() { return mList.size(); }
實現將佈局檔案對映成View的方法
/** * inflate a view by viewgroup ,id ,etc * * @param viewGroup * @param layoutId * @return */ protected View inflateItemView(ViewGroup viewGroup, int layoutId) { return inflateItemView(viewGroup, layoutId, false); } /** * inflate a view by viewgroup ,id ,etc * * @param viewGroup * @param layoutId * @param attach * @return */ protected View inflateItemView(ViewGroup viewGroup, int layoutId, boolean attach) { return LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, viewGroup, attach); }
實現onBindViewHolder方法,這一步很重要,將具體實現抽取到兩個方法中去實現,bindDataToItemView是一個抽象方法,延遲到子類實現,bindItemViewClickListener方法設定監聽器
/** * a final function to avoid you override * use template design pattern * * @param vh * @param position */ @Override public final void onBindViewHolder(VH vh, int position) { final T item = getItem(position); bindDataToItemView(vh, item); bindItemViewClickListener(vh, item); } /** * bind data to itemview * * @param vh viewholder * @param item item */ protected abstract void bindDataToItemView(VH vh, T item); /** * bind click listner to itemview * * @param vh viewholder * @param item item */ protected final void bindItemViewClickListener(VH vh, final T item) { if (mOnItemClickListener != null) { vh.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mOnItemClickListener.onClick(view, item); } }); } if (mOnItemLongClickListener != null) { vh.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mOnItemLongClickListener.onLongClick(v, item); return true; } }); } }
實現通用ViewHolder,內部使用SparseArray維持View物件
public static class SparseArrayViewHolder extends RecyclerView.ViewHolder { private final SparseArray<View> views; public SparseArrayViewHolder(View itemView) { super(itemView); views = new SparseArray<View>(); } public <T extends View> T getView(int id) { View view = views.get(id); if (view == null) { view = itemView.findViewById(id); views.put(id, view); } return (T) view; } }
內部實現了一個泛型的getView方法,其作用就是將View快取至SparseArray中去
實現通用ViewHolder的一些屬性設定方法,採用鏈式呼叫,返回this物件
public SparseArrayViewHolder setText(int viewId, String value) { TextView view = getView(viewId); view.setText(value); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setTextColor(int viewId, int textColor) { TextView view = getView(viewId); view.setTextColor(textColor); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setImageResource(int viewId, int imageResId) { ImageView view = getView(viewId); view.setImageResource(imageResId); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setBackgroundColor(int viewId, int color) { View view = getView(viewId); view.setBackgroundColor(color); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setBackgroundResource(int viewId, int backgroundRes) { View view = getView(viewId); view.setBackgroundResource(backgroundRes); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setVisible(int viewId, boolean visible) { View view = getView(viewId); view.setVisibility(visible ? View.VISIBLE : View.GONE); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setOnClickListener(int viewId, View.OnClickListener listener) { View view = getView(viewId); view.setOnClickListener(listener); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) { View view = getView(viewId); view.setOnTouchListener(listener); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) { View view = getView(viewId); view.setOnLongClickListener(listener); return SparseArrayViewHolder.this; } public SparseArrayViewHolder setTag(int viewId, Object tag) { View view = getView(viewId); view.setTag(tag); return SparseArrayViewHolder.this; }
原始碼
相關文章
- RecyclerView定製:通用ItemDecoration及全展開RecyclerView的實現View
- [kotlin]帶分類的RecyclerView通用實現新思路KotlinView
- Android 基於RecyclerView實現批量操作AndroidView
- Android使用RecyclerView實現二級列表AndroidView
- RecyclerView-->通用的AdapterViewAPT
- Android 時間軸的實現(RecyclerView更簡單)AndroidView
- Android實現帶動畫的下拉重新整理RecyclerViewAndroid動畫View
- Android實現RecyclerView巢狀流式佈局AndroidView巢狀
- Android 輕鬆實現 RecyclerView 懸浮條AndroidView
- 說說在 Android 的 RecyclerView 中如何實現下拉刷AndroidView
- [轉]Android輕鬆實現RecyclerView懸浮條AndroidView
- 為RecyclerView打造通用Adapter 讓RecyclerView更加好用ViewAPT
- Android RecyclerView實現頭部懸浮吸頂效果AndroidView
- Android中使用RecyclerView + SnapHelper實現類似ViewPager效果AndroidViewpager
- Android 實現一個通用的圓角佈局Android
- 提供一個操作便捷、多功能的Recyclerview介面卡View
- Android中的RecyclerViewAndroidView
- Android RecyclerView的使用AndroidView
- RecyclerView進階(一)RecyclerView實現雙列表聯動View
- android利用RecyclerView+自定義View實現城市選擇介面AndroidView
- 手把手教你實現Android RecyclerView上拉載入功能AndroidView
- RecyclerView實現的彈幕之中文DOCView
- 實現RecyclerView下拉重新整理View
- Kotlin 打造一個RecyclerView的通用Adapter(一)KotlinViewAPT
- Android入門教程 | RecyclerView實際使用AndroidView
- 簡潔/易用/靈活/高效->RecyclerView介面卡封裝View封裝
- Android 從零開始實現RecyclerView分組及粘性頭部效果AndroidView
- 打造基於Databinding與RecyclerView的通用Adapter類ViewAPT
- 用RecyclerView 重寫LayoutManager實現BannerViewView
- Android RecyclerView詳解AndroidView
- 使用介面實現RecyclerView中的item點選事件View事件
- 教你玩轉 Android RecyclerView:深入解析 RecyclerView ItemDecoration類AndroidView
- Android RecyclerView的簡便寫法AndroidView
- RecyclerView 之使用 ItemTouchHelper 實現互動動畫View動畫
- 自定義RecyclerView實現側滑刪除View
- 通過Data Binding為RecyclerView打造通用AdapterViewAPT
- Android中常用介面卡及定義自己的介面卡Android
- 一勞永逸——RecyclerView無型別強轉的通用ViewHolderView型別