转载:http://blog.csdn.net/lmj623565791/article/details/38902805/
通用的ViewHolder
首先分析下ViewHolder的作用,通过convertView.setTag与convertView进行绑定,然后当convertView复用时,直接从与之对于的ViewHolder(getTag)中拿到convertView布局中的控件,省去了findViewById的时间~
也就是说,实际上们每个convertView会绑定一个ViewHolder对象,这个viewHolder主要用于帮convertView存储布局中的控件。
那么我们只要写出一个通用的ViewHolder,然后对于任意的convertView,提供一个对象让其setTag即可;
既然是通用,那么我们这个ViewHolder就不可能含有各种控件的成员变量了,因为每个Item的布局是不同的,最好的方式是什么呢?
提供一个容器,专门存每个Item布局中的所有控件,而且还要能够查找出来;既然需要查找,那么ListView肯定是不行了,需要一个键值对进行保存,键为控件的Id,值为控件的引用,相信大家立刻就能想到Map;但是我们不用Map,因为有更好的替代类,就是我们android提供的SparseArray这个类,和Map类似,但是比Map效率,不过键只能为Integer.
下面看我们的ViewHolder类:
1 import android.graphics.Bitmap; 2 import android.graphics.drawable.Drawable; 3 import android.util.SparseArray; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.Button; 8 import android.widget.ImageView; 9 import android.widget.RelativeLayout; 10 import android.widget.TextView; 11 12 import com.coach.app.frame.BaseActivity; 13 import com.coach.app.frame.utils.ImageLoader; 14 import com.coach.app.widget.CircleImageView; 15 16 17 public class ViewHolder { 19 public static String Btn = "Button"; 20 public static String Txt = "TextView"; 21 22 private final SparseArray<View> mViews; 23 private int mPosition; 24 private View mConvertView; 25 public BaseActivity act; 26 27 private ViewHolder(BaseActivity act, ViewGroup parent, int layoutId, 28 int position) { 29 this.act = act; 30 this.mPosition = position; 31 this.mViews = new SparseArray<View>(); 32 mConvertView = LayoutInflater.from(act).inflate(layoutId, parent, 33 false); 34 // setTag 35 mConvertView.setTag(this); 36 } 37 38 /** 39 * 拿到一个ViewHolder对象 40 * 41 * @param act 42 * @param convertView 43 * @param parent 44 * @param layoutId 45 * @param position 46 * @return 47 */ 48 public static ViewHolder get(BaseActivity act, View convertView, 49 ViewGroup parent, int layoutId, int position) { 50 if (convertView == null || convertView.equals("")) { 51 return new ViewHolder(act, parent, layoutId, position); 52 } 53 return (ViewHolder) convertView.getTag(); 54 } 55 56 public View getConvertView() { 57 return mConvertView; 58 } 59 60 /** 61 * 通过控件的Id获取对于的控件,如果没有则加入views 62 * 63 * @param viewId 64 * @return 65 */ 66 public <T extends View> T getView(int viewId) { 67 View view = mViews.get(viewId); 68 if (view == null) { 69 view = mConvertView.findViewById(viewId); 70 mViews.put(viewId, view); 71 } 72 return (T) view; 73 } 74 75 /** 76 * 为TextView设置字符串 77 * 78 * @param viewId 79 * @param text 80 * @return 81 */ 82 public ViewHolder setText(int viewId, String type, String text) { 83 if (type.equals(Txt)) { 84 TextView view = getView(viewId); 85 view.setText(text); 86 } else if (type.equals(Btn)) { 87 Button button = getView(viewId); 88 button.setText(text); 89 } 90 return this; 91 } 92 93 94 public ViewHolder setBackgroundColor(BaseActivity act, int viewId, String type, int color) { 95 if (type.equals(Txt)) { 96 TextView view = getView(viewId); 97 view.setBackgroundColor(act.getResources().getColor(color)); 98 } else if (type.equals(Btn)) { 99 Button button = getView(viewId); 100 button.setBackgroundColor(act.getResources().getColor(color)); 101 } else if (type.equals("RelativeLayout")) { 102 RelativeLayout relative = getView(viewId); 103 relative.setBackgroundColor(act.getResources().getColor(color)); 104 105 } 106 return this; 107 } 108 109 110 public ViewHolder setBackgroundDrawable(BaseActivity act, int viewId, String type, int drawable) { 111 if (type.equals(Txt)) { 112 TextView view = getView(viewId); 113 view.setBackgroundDrawable(act.getResources().getDrawable(drawable)); 114 } else if (type.equals(Btn)) { 115 Button button = getView(viewId); 116 button.setBackgroundDrawable(act.getResources().getDrawable(drawable)); 117 } else if (type.equals("RelativeLayout")) { 118 RelativeLayout relative = getView(viewId); 119 relative.setBackgroundDrawable(act.getResources().getDrawable(drawable)); 120 121 } 122 return this; 123 } 124 125 126 127 public ViewHolder setDisplay(BaseActivity act, int viewId, String type, String url) { 128 if (type.equals("CircleImageView")) { 129 CircleImageView view = getView(viewId); 130 act.mToolBitmap.display(view, url); 131 } 132 133 return this; 134 } 135 136 137 public ViewHolder setTextColor(BaseActivity act, int viewId, String type, int color) { 138 if (type.equals(Txt)) { 139 TextView view = getView(viewId); 140 view.setTextColor(act.getResources().getColor(color)); 141 } else if (type.equals(Btn)) { 142 Button button = getView(viewId); 143 button.setTextColor(act.getResources().getColor(color)); 144 } 145 return this; 146 } 147 148 149 public ViewHolder setFocusable(int viewId, boolean flag) { 150 Button button = getView(viewId); 151 button.setFocusable(flag); 152 return this; 153 } 154 155 156 public ViewHolder setEnabled(int viewId, boolean flag) { 157 Button button = getView(viewId); 158 button.setEnabled(flag); 159 return this; 160 } 161 168 /*public ViewHolder setTextOnListener(int viewId, OnClickListener listener) { 169 TextView view = getView(viewId); 170 view.setOnClickListener(listener); 171 return this; 172 } 173 174 public ViewHolder setBtnOnListener(int viewId, OnClickListener listener) { 175 Button btn = getView(viewId); 176 btn.setOnClickListener(listener); 177 return this; 178 } 179 180 public ViewHolder setImageViewOnListener(int viewId, OnClickListener listener) { 181 ImageView img = getView(viewId); 182 img.setOnClickListener(listener); 183 return this; 184 }*/ 185 186 187 /** 188 * 为ImageView设置图片 189 * 190 * 尽量不使用 191 */ 192 public ViewHolder setImageResource(int viewId, int drawableId) { 193 ImageView view = getView(viewId); 194 view.setImageResource(drawableId); 195 196 return this; 197 } 198 199 public ViewHolder setImageDrawable(int viewId, Drawable drawable) { 200 ImageView view = getView(viewId); 201 view.setImageDrawable(drawable); 202 203 return this; 204 } 205 206 /** 207 * 为ImageView设置图片 208 * 209 * @param viewId 210 * @param bm 211 * @return 212 */ 213 public ViewHolder setImageBitmap(int viewId, Bitmap bm) { 214 ImageView view = getView(viewId); 215 view.setImageBitmap(bm); 216 return this; 217 } 218 219 /** 220 * 为ImageView设置图片 221 * 222 * @param viewId 223 * @param url 224 * @return 225 */ 226 public ViewHolder setImageByUrl(int viewId, String url) { 227 ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(url, 228 (ImageView) getView(viewId)); 229 return this; 230 } 231 232 public int getPosition() { 233 return mPosition; 234 } 235 236 }
与传统的ViewHolder不同,我们使用了一个SparseArray<View>用于存储与之对于的convertView的所有的控件,当需要拿这些控件时,通过getView(id)进行获取;
下面看使用该ViewHolder的MyAdapter;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//实例化一个viewHolder
ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent,
R.layout.item_single_str, position);
//通过getView获取控件
TextView tv = viewHolder.getView(R.id.id_tv_title);
//使用
tv.setText(mDatas.get(position));
return viewHolder.getConvertView();
}
只看getView,其他方法都一样;首先调用ViewHolder的get方法,如果convertView为null,new一个ViewHolder实例,通过使用mInflater.inflate加载布局,然后new一个SparseArray用于存储View,最后setTag(this);
如果存在那么直接getTag
最后通过getView(id)获取控件,如果存在则直接返回,否则调用findViewById,返回存储,返回。
好了,一个通用的ViewHolder写好了,以后一个项目几十个Adapter一个ViewHolder直接hold住全场~~
打造通用的Adapter
有了通用的ViewHolder大家肯定不能满足,怎么也得省出dota的时间,人在塔在~~
下面看如何打造一个通过的Adapter,我们叫做CommonAdapter
继续分析,Adapter一般需要保持一个List对象,存储一个Bean的集合,不同的ListView,Bean肯定是不同的,这个CommonAdapter肯定需要支持泛型,内部维持一个List<T>,就解决我们的问题了;
1 import android.view.LayoutInflater; 2 import android.view.View; 3 import android.view.ViewGroup; 4 import android.widget.BaseAdapter; 5 import com.coach.app.frame.BaseActivity; 6 import java.util.List; 7 8 public abstract class AdapterUtils<T> extends BaseAdapter { 9 protected LayoutInflater mInflater; 10 //protected Context mContext; 11 protected BaseActivity act; 12 protected List<T> mDatas; 13 protected final int mItemLayoutId; 14 15 public AdapterUtils(BaseActivity act, List<T> mDatas, int itemLayoutId) { 16 this.act = act; 17 this.mInflater = LayoutInflater.from(act); 18 this.mDatas = mDatas; 19 this.mItemLayoutId = itemLayoutId; 20 } 21 24 @Override 25 public int getCount() { 26 return mDatas.size(); 27 } 28 29 @Override 30 public T getItem(int position) { 31 return mDatas.get(position); 32 } 33 34 @Override 35 public long getItemId(int position) { 36 return position; 37 } 38 39 @Override 40 public View getView(int position, View convertView, ViewGroup parent) { 42 final ViewHolder viewHolder = getViewHolder(position, convertView, 43 parent); 45 convert(viewHolder, getItem(position)); // .position 47 return viewHolder.getConvertView(); 48 } 49 50 public abstract void convert(ViewHolder helper, T item); // ,int position 51 52 public ViewHolder getViewHolder(int position, View convertView, 53 ViewGroup parent) { 54 return ViewHolder.get(act, convertView, parent, mItemLayoutId, 55 position); 56 } 58 }