下面我把自己优化的万能ViewHolder和Adapter跟大家分享一下,也为自己留一个记录。话不多说,直接上代码:
Viewholder:
package com.example.xf.test.adapter; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class ViewHolder { private View convertView; private int position; //SparseArray是一个map,但是它比map效率更高,实现了integer与View的键值关系 private SparseArray<View> mArray; private ViewHolder(Context context, View convertView, ViewGroup parent, int position, int layoutId) { this.convertView = convertView; this.position = position; this.convertView = LayoutInflater.from(context).inflate(layoutId, parent, false); this.convertView.setTag(this); mArray = new SparseArray<View>(); } //在adapter中使用这个方法去创建ViewHolder,需要先判断convertView是否为空 public static ViewHolder get(Context context, View convertView, ViewGroup parent, int position, int layoutId) { if (convertView == null) { return new ViewHolder(context, convertView, parent, position, layoutId); } else { ViewHolder holder = (ViewHolder) convertView.getTag(); holder.position = position; return holder; } } //取得item布局中的id为viewId的控件 public <T extends View> T getView(int viewId) { View view = mArray.get(viewId); if (view == null) { view = convertView.findViewById(viewId); mArray.put(viewId, view); } return (T) view; } public View getConvertView() { return convertView; } //为TextView设置文本 public ViewHolder setText(int viewId,String text){ TextView view = getView(viewId); view.setText(text); return this; } }CommonAdapter(注意这个类是abstract):
package com.example.xf.test.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.List; /** * Created by xf on 2015/4/18. */ public abstract class CommonAdapter<T> extends BaseAdapter { private Context context; private List<T> list; private int layoutId; public CommonAdapter(Context context, List<T> list, int layoutId) { this.context = context; this.list = list; this.layoutId = layoutId; } @Override public Object getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = ViewHolder.get(context, convertView, parent, position, layoutId); convert(holder, list.get(position)); return holder.getConvertView(); } //将此方法开放出去,在adapter中实现 public abstract void convert(ViewHolder holder, T t); }
实际项目中需要使用的Adapter是继承这个CommonAdapter的:
package com.example.xf.test.adapter; import android.content.Context; import com.example.xf.test.R; import com.example.xf.test.pojo.Bean; import java.util.List; /** * Created by xf on 2015/4/19. */ public class CommonAdapterImpl extends CommonAdapter<Bean> { public CommonAdapterImpl(Context context, List<Bean> list, int layoutId) { super(context, list, layoutId); } @Override public void convert(ViewHolder holder, Bean bean) { holder.setText(R.id.id_textview1, bean.getDestription()).setText(R.id.id_textview2, bean.getName()); } }也就是说每个ListView只需要写这么几行的代码就能解决战斗,是不是很爽!
2.理解泛型,在我自己理解泛型就是可以让你在构建代码的时候不指定类型,使用的时候在指定。
Demo地址:http://download.csdn.net/detail/xf396191344/9264617