Android BaseAdapter
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter { private List<T> mDataList; public BaseAdapter(List<T> list) { mDataList = list; } @Override public int getCount() { return mDataList.size(); } @Override public T getItem(int position) { return position < mDataList.size() ? mDataList.get(position) : null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = ViewHolder.getInstance(convertView, parent, getItemLayoutId(position)); T item = getItem(position); convert(viewHolder, item, position, parent); return viewHolder.getConvertView(); } public List<T> getDataList() { return mDataList; } public abstract @LayoutRes int getItemLayoutId(int position); public abstract void convert(ViewHolder holder, T item, int position, ViewGroup parent); /** * 重置資料 * @param data */ public void resetData(List<T> data) { if (data == null) { return; } mDataList = data; super.notifyDataSetChanged(); }
}
public class ViewHolder { private View mConvertView; private SparseArray<View> mViews; private ViewHolder(View convertView) { mViews = new SparseArray<>(); this.mConvertView = convertView; convertView.setTag(this); } /** * 獲取ViewHolder 例項物件 * * @param convertView * @param parent * @param layoutId * @return */ public static ViewHolder getInstance(View convertView, ViewGroup parent, int layoutId) { if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false); return new ViewHolder(convertView); } return (ViewHolder) convertView.getTag(); } public View getConvertView() { return mConvertView; } public <T extends View> T getView(int viewId) { return retrieveView(viewId); } /** * 設定控制元件是否可見 * * @param viewId * @param isVisible true 或者 false, 如設為false 則控制元件狀態為 GONE * @return */ public ViewHolder setVisible(int viewId, boolean isVisible) { return setVisibility(viewId, isVisible ? View.VISIBLE : View.GONE); } public ViewHolder setVisibility(int viewId, int visibility) { View view = getView(viewId); view.setVisibility(visibility); return this; } public ViewHolder setBackgroundColor(int viewId, int color) { View view = getView(viewId); view.setBackgroundColor(color); return this; } public ViewHolder setBackgroundRes(int viewId, int resid) { View view = getView(viewId); view.setBackgroundResource(resid); return this; } /** * 設定透明度 * * @param viewId * @param alpha 0 ~ 1, 0 完全透明 * @return */ public ViewHolder setAlpha(int viewId, int alpha) { View view = getView(viewId); view.setAlpha(alpha); return this; } public ViewHolder setText(int viewId, CharSequence text) { TextView tv = getView(viewId); tv.setText(text); return this; } public ViewHolder setText(int viewId, CharSequence text, int color) { TextView tv = getView(viewId); tv.setText(text); tv.setTextColor(color); return this; } public ViewHolder setText(int viewId, CharSequence text, int color, float size) { TextView tv = getView(viewId); tv.setText(text); tv.setTextColor(color); tv.setTextSize(size); return this; } @SuppressWarnings("deprecation") public ViewHolder setTextColorRes(int viewId, int resId) { return setTextColor(viewId, mConvertView.getContext().getResources().getColor(resId)); } public ViewHolder setTextColor(int viewId, int color) { TextView tv = getView(viewId); tv.setTextColor(color); return this; } public ViewHolder setTextSize(int viewId, float size) { TextView tv = getView(viewId); tv.setTextSize(size); return this; } public ViewHolder setImage(int viewId, int resId) { ImageView imageView = getView(viewId); imageView.setImageResource(resId); return this; } public ViewHolder setImage(int viewId, Drawable drawable) { ImageView imageView = getView(viewId); imageView.setImageDrawable(drawable); return this; } public ViewHolder setImage(int viewId, Bitmap bmp) { ImageView imageView = getView(viewId); imageView.setImageBitmap(bmp); return this; } public ViewHolder setTypeface(int viewId, Typeface typeface) { TextView view = retrieveView(viewId); view.setTypeface(typeface); view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); return this; } public ViewHolder setTag(int viewId, Object tag) { View view = retrieveView(viewId); view.setTag(tag); return this; } public ViewHolder setTag(int viewId, int key, Object tag) { View view = retrieveView(viewId); view.setTag(key, tag); return this; } public ViewHolder setChecked(int viewId, boolean checked) { Checkable view = (Checkable) retrieveView(viewId); view.setChecked(checked); return this; } public ViewHolder setSelected(int viewId, boolean selected) { View view = retrieveView(viewId); view.setSelected(selected); return this; } @SuppressWarnings("unchecked") private <T extends View> T retrieveView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } }
RecycleView版本 RecycleViewHolder
public class RecycleViewHolder extends RecyclerView.ViewHolder { private View mRootView; private SparseArray<View> mViews; public RecycleViewHolder(View view) { super(view); mViews = new SparseArray<>(); this.mRootView = view; } public View getRootView(){ return mRootView; } public <T extends View> T getView(int viewId) { return retrieveView(viewId); } public ImageView getImageView(int viewId){ return getView(viewId); } /** * 設定控制元件是否可見 * * @param viewId * @param isVisible true 或者 false, 如設為false 則控制元件狀態為 GONE * @return */ public RecycleViewHolder setVisible(int viewId, boolean isVisible) { return setVisibility(viewId, isVisible ? View.VISIBLE : View.GONE); } public RecycleViewHolder setVisibility(int viewId, int visibility) { View view = getView(viewId); view.setVisibility(visibility); return this; } public RecycleViewHolder setBackgroundColor(int viewId, int color) { View view = getView(viewId); view.setBackgroundColor(color); return this; } public RecycleViewHolder setBackgroundRes(int viewId, int resid) { View view = getView(viewId); view.setBackgroundResource(resid); return this; } public RecycleViewHolder setText(int viewId, CharSequence text) { TextView tv = getView(viewId); tv.setText(text); return this; } public RecycleViewHolder setText(int viewId, CharSequence text, int color) { TextView tv = getView(viewId); tv.setText(text); tv.setTextColor(color); return this; } public RecycleViewHolder setText(int viewId, CharSequence text, int color, float size) { TextView tv = getView(viewId); tv.setText(text); tv.setTextColor(color); tv.setTextSize(size); return this; } public RecycleViewHolder setTextColor(int viewId, int color) { TextView tv = getView(viewId); tv.setTextColor(color); return this; } public RecycleViewHolder setTextSize(int viewId, float size) { TextView tv = getView(viewId); tv.setTextSize(size); return this; } public RecycleViewHolder setImage(int viewId, int resId) { ImageView imageView = getView(viewId); imageView.setImageResource(resId); return this; } public RecycleViewHolder setImage(int viewId, Drawable drawable) { ImageView imageView = getView(viewId); imageView.setImageDrawable(drawable); return this; } public RecycleViewHolder setImage(int viewId, Bitmap bmp) { ImageView imageView = getView(viewId); imageView.setImageBitmap(bmp); return this; } public RecycleViewHolder setTypeface(int viewId, Typeface typeface) { TextView view = retrieveView(viewId); view.setTypeface(typeface); view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); return this; } public RecycleViewHolder setTag(int viewId, Object tag) { View view = retrieveView(viewId); view.setTag(tag); return this; } public RecycleViewHolder setTag(int viewId, int key, Object tag) { View view = retrieveView(viewId); view.setTag(key, tag); return this; } public RecycleViewHolder setChecked(int viewId, boolean checked) { Checkable view = (Checkable) retrieveView(viewId); view.setChecked(checked); return this; } public RecycleViewHolder setSelected(int viewId, boolean selected) { View view = retrieveView(viewId); view.setSelected(selected); return this; } private <T extends View> T retrieveView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mRootView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } }
相關文章
- Android 自定義 BaseAdapter 最佳實踐AndroidAPT
- BaseAdapter + GalleryAPT
- arrayadapter simpleadapter baseadapterAPT
- android開發 BaseAdapter中getView()裡的3個引數的含義AndroidAPTView
- android開發 BaseAdapter中getView()裡的3個引數是什麼意思AndroidAPTView
- Android開發 - (介面卡)Adapter類中BaseAdapter實現類詳細解析AndroidAPT
- 孟老闆 BaseAdapter封裝(五) ListAdapterAPT封裝
- Android學習—— Android佈局Android
- Android之android exported="false"作用AndroidExportFalse
- [Android元件化]Android app BundleAndroid元件化APP
- 聚焦 Android 11: Android 開發者工具Android
- 【Android】Android設計準則Android
- Android Transition(Android過渡動畫)Android動畫
- 愛上Android之初識AndroidAndroid
- [Android]Android原始碼下載Android原始碼
- [android]android命令列截圖Android命令列
- 迴歸Android Focus on Android.Android
- AndroidAndroid
- android:id="@android:id/tabhost" 、android:id="@+id/llRoot" 、android:id="@id/llRoot" 之間的區別Android
- android開發 之 Bolts-AndroidAndroid
- Android總結篇系列:Android ServiceAndroid
- Android APIs (Class Index - Android SDK)(一)AndroidAPIIndex
- Android APIs (Class Index - Android SDK)(二)AndroidAPIIndex
- Android元件---重新認識Android(2)Android元件
- 【Android】Android 簽名相關問題Android
- Android 面試(五):探索 Android 的 HandlerAndroid面試
- 【Android】神奇的android:clipChildren屬性Android
- 【Android】Android輸入子系統Android
- [Android]calabash-android再深入Android
- android之 Android訊息機制Android
- [android]android自動化測試Android
- [android]android-am命令的使用Android
- [Android]android自動化測試十六之calabash-androidAndroid
- Android Wear-Detecting Location on Android Wear,Requesting Permissions on Android WearAndroid
- [Android Pro] android 4.4 Android原生許可權管理:AppOpsAndroidAPP
- android之android:paddingLeft和android:layout_marginLeft區別: 分類: Android android開發實踐 Android入門教程 2011-0...Androidpadding
- 【Android SDK】在命令列管理Android SDKAndroid命令列
- Android之Service設定android:process作用Android