1.封装ViewHodler
1 package com.example.utils; 2 3 import android.content.Context; 4 import android.graphics.Bitmap; 5 import android.util.SparseArray; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.ImageView; 10 import android.widget.TextView; 11 12 public class ViewHolder { 13 14 private SparseArray<View> mViews; 15 private int mPostion; 16 private View mConverView; 17 private View view; 18 19 public ViewHolder(Context context, View converview, ViewGroup parent, 20 int layoutid, int postion) { 21 22 this.mPostion = postion; 23 mConverView = LayoutInflater.from(context).inflate(layoutid, parent,false); 24 mViews = new SparseArray<View>(); 25 mConverView.setTag(this); 26 } 27 28 public static ViewHolder get(Context context, View converView, 29 ViewGroup parent, int layoutid, int postion) { 30 31 if (converView == null) { 32 return new ViewHolder(context, converView, parent, layoutid,postion); 33 } else { 34 ViewHolder holder = (ViewHolder) converView.getTag(); 35 holder.mPostion = postion; 36 return holder; 37 } 38 39 } 40 41 public View getConverView() { 42 return mConverView; 43 } 44 45 /** 46 * @author 通过id获取控件 47 * 48 * */ 49 50 public <T extends View> T getView(int viewId) { 51 52 View view = mViews.get(viewId); 53 if (view == null) { 54 view = mConverView.findViewById(viewId); 55 mViews.put(viewId, view); 56 } 57 return (T) view; 58 59 } 60 public ViewHolder getTextView(int id,String tv){ 61 62 TextView mTextView = getView(id); 63 mTextView.setText(tv); 64 return this; 65 } 66 67 public ViewHolder getImageResoure(int id,int color){ 68 69 ImageView img=getView(id); 70 img.setImageResource(color); 71 return this; 72 } 73 74 public ViewHolder getImageBitmap(int id,Bitmap map){ 75 76 ImageView view=getView(id); 77 view.setImageBitmap(map); 78 return this; 79 } 80 }
2. 封装BaseAdapter
1 package com.example.utils; 2 3 import java.util.List; 4 import android.content.Context; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 10 public abstract class CommonAdapter<T> extends BaseAdapter { 11 12 protected Context mContext; 13 protected List<T> mDatas; 14 protected LayoutInflater mInflater; 15 protected int mlayoutId; 16 public CommonAdapter(Context context,List<T> data,int layoutId) { 17 18 this.mContext=context; 19 this.mDatas=data; 20 mInflater=LayoutInflater.from(mContext); 21 this.mlayoutId=layoutId; 22 } 23 24 @Override 25 public int getCount() { 26 return mDatas.size(); 27 } 28 29 @Override 30 public T getItem(int postion) { 31 return mDatas.get(postion); 32 } 33 34 @Override 35 public long getItemId(int postion) { 36 return postion; 37 } 38 39 @Override 40 public View getView(int postion, View converview, ViewGroup group){ 41 ViewHolder holder=ViewHolder.get(mContext, converview, group, mlayoutId, postion); 42 convert(holder, getItem(postion)); 43 return holder.getConverView(); 44 }; 45 46 public abstract void convert(ViewHolder holder,T t); 47 }
3.自己的MyAdapter
1 package com.example.utils; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import com.example.adapter.R; 7 import com.example.bean.Bean; 8 9 public class MyAdapter extends CommonAdapter<Bean>{ 10 11 public MyAdapter(Context context, List<Bean> data, int layoutId) { 12 super(context, data, layoutId); 13 } 14 15 @Override 16 public void convert(ViewHolder holder, Bean bean) { 17 18 holder.getTextView(R.id.tv_title, bean.getTitle()); 19 holder.getTextView(R.id.tv_time, bean.getTime()); 20 holder.getTextView(R.id.tv_from, bean.getForm()); 21 } 22 }
时间: 2024-12-05 18:28:57