在android中我们经常用到adapter适配器,今天在网上看到有许多人都写得有,自己参考了一些,现在自己也写出来放在自己的博客中,方便以后回顾 代码:`
public abstract class CommoAdapter extends BaseAdapter{
/** * 上下文 */PRotected Context context;/** * 数据源 */protected List<T> listDatas;/** * Item布局ID */protected int layoutId;public CommoAdapter(Context context, List<T> listDatas, int layoutId){ this.context=context; this.listDatas=listDatas; this.layoutId=layoutId;}@Overridepublic int getCount() { return listDatas==null?0:listDatas.size();}@Overridepublic Object getItem(int position) { return position;}@Overridepublic long getItemId(int position) { return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { ViewHouder holder = ViewHouder.getViewHolder(context, convertView, parent, layoutId); fillData(holder, position); return holder.getMConvertView();}/** * 填充数据 * * @param holder * @param position */protected abstract void fillData(ViewHouder holder, int position);` Viewhouder工具类
public class ViewHouder { /** * View容器,用于存放Holer中的View 的SparseArray * 是Android推荐使用的一个优化容器,相当于一个Map<integer,View> */ private SparseArray<View> mViews; /** * Item布局View convertView */ private View mConvertView; public ViewHouder(Context context, ViewGroup parent, int layoutId) { mViews = new SparseArray<View>(); mConvertView = LayoutInflater.from(context).inflate(layoutId, null); mConvertView.setTag(this); } /** * 获取ViewHolder * * @param context * 上下文 * @param convertView * @param parent * @param layoutId * 布局layout Id * @param * @return */ public static ViewHouder getViewHolder(Context context, View convertView, ViewGroup parent, int layoutId) { if (convertView == null) return new ViewHouder(context, parent, layoutId); return (ViewHouder) convertView.getTag(); } /** * * 获取Holder中的ItemView * * @param viewId * @return */ @SuppressWarnings("unchecked") public <T extends View> T getView(int viewId) { View item = mViews.get(viewId); if (item == null) { item = mConvertView.findViewById(viewId); mViews.put(viewId, item); } return (T) item; } /** * 设置TextView 的文本 * * @param viewId * @param text */ public TextView setTVText(int viewId, String text) { TextView tv = getView(viewId); tv.setText(text); return tv; } public TextView setsetBackground(int viewId,int color){ TextView tv=getView(viewId); return tv; } /** * 设置EditText 的文本 * * @param viewId * @param text */ public EditText setETText(int viewId, String text) { EditText et = getView(viewId); et.setText(text); return et; } /** * 设置TextView 的文本 * * @param viewId// * @param text */ public ImageView setIVBg(int viewId, int imgId) { ImageView iv = getView(viewId); iv.setImageResource(imgId); return iv; } /** * 设置TextView 的文本 * * @param viewId * @param text */ public void setTextView(int viewId, String text) { TextView tv = getView(viewId); tv.setText(text); } /** * 获取convertView * * @return */ public View getMConvertView() { return mConvertView; }}自己的adapter,列如名字叫MessgeAdapter
public class MessgeAdapter extends CommoAdapter<String>{ public MessgeAdapter(Context context, List<String> listDatas, int layoutId) { super(context, listDatas, layoutId); } @Override protected void fillData(ViewHouder holder, int position) { TextView tvMessgeTitle=holder.getView(R.id.tvMessgeTitle); tvMessgeTime.setText(TimeFormat.DateFormat(listDatas.get(position))); }}在Activity中使用 List list; ListView listview;
MessgeAdapter adapter=new MessgeAdapter (this,list,R.layout.item_messge); listview.setadapter(adapter);
新闻热点
疑难解答