转载: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类:
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView; import com.coach.app.frame.BaseActivity;
import com.coach.app.frame.utils.ImageLoader;
import com.coach.app.widget.CircleImageView; public class ViewHolder {
public static String Btn = "Button";
public static String Txt = "TextView"; private final SparseArray<View> mViews;
private int mPosition;
private View mConvertView;
public BaseActivity act; private ViewHolder(BaseActivity act, ViewGroup parent, int layoutId,
int position) {
this.act = act;
this.mPosition = position;
this.mViews = new SparseArray<View>();
mConvertView = LayoutInflater.from(act).inflate(layoutId, parent,
false);
// setTag
mConvertView.setTag(this);
} /**
* 拿到一个ViewHolder对象
*
* @param act
* @param convertView
* @param parent
* @param layoutId
* @param position
* @return
*/
public static ViewHolder get(BaseActivity act, View convertView,
ViewGroup parent, int layoutId, int position) {
if (convertView == null || convertView.equals("")) {
return new ViewHolder(act, parent, layoutId, position);
}
return (ViewHolder) convertView.getTag();
} public View getConvertView() {
return mConvertView;
} /**
* 通过控件的Id获取对于的控件,如果没有则加入views
*
* @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;
} /**
* 为TextView设置字符串
*
* @param viewId
* @param text
* @return
*/
public ViewHolder setText(int viewId, String type, String text) {
if (type.equals(Txt)) {
TextView view = getView(viewId);
view.setText(text);
} else if (type.equals(Btn)) {
Button button = getView(viewId);
button.setText(text);
}
return this;
} public ViewHolder setBackgroundColor(BaseActivity act, int viewId, String type, int color) {
if (type.equals(Txt)) {
TextView view = getView(viewId);
view.setBackgroundColor(act.getResources().getColor(color));
} else if (type.equals(Btn)) {
Button button = getView(viewId);
button.setBackgroundColor(act.getResources().getColor(color));
} else if (type.equals("RelativeLayout")) {
RelativeLayout relative = getView(viewId);
relative.setBackgroundColor(act.getResources().getColor(color)); }
return this;
} public ViewHolder setBackgroundDrawable(BaseActivity act, int viewId, String type, int drawable) {
if (type.equals(Txt)) {
TextView view = getView(viewId);
view.setBackgroundDrawable(act.getResources().getDrawable(drawable));
} else if (type.equals(Btn)) {
Button button = getView(viewId);
button.setBackgroundDrawable(act.getResources().getDrawable(drawable));
} else if (type.equals("RelativeLayout")) {
RelativeLayout relative = getView(viewId);
relative.setBackgroundDrawable(act.getResources().getDrawable(drawable)); }
return this;
} public ViewHolder setDisplay(BaseActivity act, int viewId, String type, String url) {
if (type.equals("CircleImageView")) {
CircleImageView view = getView(viewId);
act.mToolBitmap.display(view, url);
} return this;
} public ViewHolder setTextColor(BaseActivity act, int viewId, String type, int color) {
if (type.equals(Txt)) {
TextView view = getView(viewId);
view.setTextColor(act.getResources().getColor(color));
} else if (type.equals(Btn)) {
Button button = getView(viewId);
button.setTextColor(act.getResources().getColor(color));
}
return this;
} public ViewHolder setFocusable(int viewId, boolean flag) {
Button button = getView(viewId);
button.setFocusable(flag);
return this;
} public ViewHolder setEnabled(int viewId, boolean flag) {
Button button = getView(viewId);
button.setEnabled(flag);
return this;
} /*public ViewHolder setTextOnListener(int viewId, OnClickListener listener) {
TextView view = getView(viewId);
view.setOnClickListener(listener);
return this;
} public ViewHolder setBtnOnListener(int viewId, OnClickListener listener) {
Button btn = getView(viewId);
btn.setOnClickListener(listener);
return this;
} public ViewHolder setImageViewOnListener(int viewId, OnClickListener listener) {
ImageView img = getView(viewId);
img.setOnClickListener(listener);
return this;
}*/ /**
* 为ImageView设置图片
*
* 尽量不使用
*/
public ViewHolder setImageResource(int viewId, int drawableId) {
ImageView view = getView(viewId);
view.setImageResource(drawableId); return this;
} public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
ImageView view = getView(viewId);
view.setImageDrawable(drawable); return this;
} /**
* 为ImageView设置图片
*
* @param viewId
* @param bm
* @return
*/
public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this;
} /**
* 为ImageView设置图片
*
* @param viewId
* @param url
* @return
*/
public ViewHolder setImageByUrl(int viewId, String url) {
ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(url,
(ImageView) getView(viewId));
return this;
} public int getPosition() {
return mPosition;
} }
与传统的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>,就解决我们的问题了;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.coach.app.frame.BaseActivity;
import java.util.List; public abstract class AdapterUtils<T> extends BaseAdapter {
protected LayoutInflater mInflater;
//protected Context mContext;
protected BaseActivity act;
protected List<T> mDatas;
protected final int mItemLayoutId; public AdapterUtils(BaseActivity act, List<T> mDatas, int itemLayoutId) {
this.act = act;
this.mInflater = LayoutInflater.from(act);
this.mDatas = mDatas;
this.mItemLayoutId = itemLayoutId;
} @Override
public int getCount() {
return mDatas.size();
} @Override
public T getItem(int position) {
return mDatas.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder = getViewHolder(position, convertView,
parent);
convert(viewHolder, getItem(position)); // .position
return viewHolder.getConvertView();
} public abstract void convert(ViewHolder helper, T item); // ,int position public ViewHolder getViewHolder(int position, View convertView,
ViewGroup parent) {
return ViewHolder.get(act, convertView, parent, mItemLayoutId,
position);
}
}