我们平常使用Adapter的方式
public class BusbaseSearchApadter extends SimpleBaseApadter { private List<Busbase> busbases = new ArrayList<Busbase>(); public Context context; public void setList(List<Busbase> busbases) { this.busbases = busbases; } public BusbaseSearchApadter(Context context) { super(context); this.context = context; } @Override public int getCount() { return busbases.size(); } @Override public Object getItem(int position) { return busbases.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Busbase busbase = busbases.get(position); ViewHolder holder = null; if(convertView == null){ holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.hx_item_search_bus, parent, false); holder.hx_item_bus_search_image = (ImageView)convertView.findViewById(R.id.hx_item_bus_search_image); holder.hx_item_bus_search_name = (TextView)convertView.findViewById(R.id.hx_item_bus_search_name); holder.hx_item_bus_search_mobile = (TextView)convertView.findViewById(R.id.hx_item_bus_search_mobile); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView .getTag(); } bitmapUtils.display(holder.hx_item_bus_search_image, busbase.getShopLogo()); holder.hx_item_bus_search_name.setText(busbase.name); holder.hx_item_bus_search_mobile.setText(busbase.mobile); return convertView; } class ViewHolder{ ImageView hx_item_bus_search_image; TextView hx_item_bus_search_name; TextView hx_item_bus_search_mobile; }
可以看出其中 有用的代码只有只有getView 和ViewHolder 但是其他的代码又不得不写,太痛苦 了不得不重复的写无数adapter 和ViewHoder.
我们可以写一个通用的ViewHolder
public class ViewHolder { private final SparseArray<View> mViews; private int mPosition; private View mConvertView; protected BitmapUtils bitmapUtils; private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) { bitmapUtils = new BitmapUtils(context); bitmapUtils.configDefaultLoadingImage(R.drawable.hx_loodding); bitmapUtils.configDefaultLoadFailedImage(R.drawable.hx_loodding); bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565); this.mPosition = position; this.mViews = new SparseArray<View>(); mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false); // setTag mConvertView.setTag(this); } /** * 拿到一个ViewHolder对象 * * @param context * @param convertView * @param parent * @param layoutId * @param position * @return */ public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) { if (convertView == null) { return new ViewHolder(context, 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 text) { TextView view = getView(viewId); view.setText(text); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageResource(int viewId, int drawableId) { ImageView view = getView(viewId); view.setImageResource(drawableId); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageBitmap(int viewId, Bitmap bm) { ImageView view = getView(viewId); view.setImageBitmap(bm); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageByUrl(int viewId, String url) { bitmapUtils.display((ImageView) getView(viewId), url); return this; } public int getPosition() { return mPosition; }
使用一个SparseArray将View存起来
现在Adapter这样写
public abstract class AbstuctBaseApadter<T> extends SimpleBaseApadter { protected LayoutInflater mInflater; protected Context mContext; protected List<T> mDatas; protected int mItemLayoutId; public AbstuctBaseApadter(Context context) { super(context); } public AbstuctBaseApadter(Context context, List<T> mDatas, int itemLayoutId) { this(context); this.mContext = context; this.mInflater = LayoutInflater.from(mContext); this.mDatas = mDatas; this.mItemLayoutId = itemLayoutId; } public void setList(List<T> mDatas){ this.mDatas = mDatas; } @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)); return viewHolder.getConvertView(); } public abstract void convert(ViewHolder helper, T item); private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) { return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position); }
现在我们只需要重写 convert方法 看activity里面怎么使用
AbstuctBaseApadter abstuctBaseApadter = new AbstuctBaseApadter<Map<String,Object>>(activity,list_map,R.layout.hx_money_item) { public void convert(ViewHolder helper, Map<String, Object> item) { helper.setText(R.id.id_item_1, item.get("time").toString()); helper.setText(R.id.id_item_3, item.get("sourcename").toString()); } };
哈哈 打工改成
版权声明:本文为博主原创文章,未经博主允许不得转载。