博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BaseAdapter的优化
阅读量:5011 次
发布时间:2019-06-12

本文共 9665 字,大约阅读时间需要 32 分钟。

传统的
1 package cct.commonadapter.bean; 2 import android.content.Context; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.TextView; 8 import java.util.List; 9 import cct.commonadapter.R;10 11 public class MyAdapter extends BaseAdapter {12     private LayoutInflater inflater;13     private List
mdata;14 public MyAdapter(Context context,List
mdata){15 inflater=LayoutInflater.from(context);16 this.mdata=mdata;17 }18 @Override19 public int getCount() {20 return mdata.size();21 }22 @Override23 public Object getItem(int position) {24 return mdata.get(position);25 }26 @Override27 public long getItemId(int position) {28 return position;29 }30 @Override31 public View getView(int position, View convertView, ViewGroup parent) {32 ViewHolders holder;33 if(convertView==null){34 convertView=inflater.inflate(R.layout.item,parent,false);35 holder=new ViewHolders();36 holder.mtitle= (TextView) convertView.findViewById(R.id.mtitle);37 holder.mdesc= (TextView) convertView.findViewById(R.id.mdesc);38 holder.mtime= (TextView) convertView.findViewById(R.id.mtime);39 holder.mphone= (TextView) convertView.findViewById(R.id.mphone);40 convertView.setTag(holder);41 }42 else {43 holder = (ViewHolders) convertView.getTag();44 }45 Message ms = mdata.get(position);46 holder.mtitle.setText(ms.getTitle());47 holder.mdesc.setText(ms.getDesc());48 holder.mtime.setText(ms.getTime());49 holder.mphone.setText(ms.getPhone());50 51 return convertView;52 }53 private class ViewHolders54 {55 TextView mtitle;56 TextView mdesc;57 TextView mtime;58 TextView mphone;59 }60 }

 附上Msessage类

1     public class Message { 2     private String title; 3     private String desc; 4     private String time; 5     private String phone; 6     public Message(int position) { 7     } 8     public Message(String title, String desc, String time, String phone) { 9         this.title = title;10         this.desc = desc;11         this.time = time;12         this.phone = phone;13     }14     public String getDesc() {15         return desc;16     }17 18     public void setDesc(String desc) {19         this.desc = desc;20     }21 22     public String getTitle() {23         return title;24     }25 26     public void setTitle(String title) {27         this.title = title;28     }29 30     public String getTime() {31         return time;32     }33 34     public void setTime(String time) {35         this.time = time;36     }37 38     public String getPhone() {39         return phone;40     }41 42     public void setPhone(String phone) {43         this.phone = phone;44     }45 46 }

 

万能适配器
//CommonAdapter  1 package cct.commonadapter.adapterUtil; 2 import android.content.Context; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import java.util.List; 8 import cct.commonadapter.R; 9 10 public  abstract class CommonAapter
extends BaseAdapter {11 protected Context context;12 protected LayoutInflater inflater;13 protected List
mdata;14 15 public CommonAapter(Context context, List
mdata) {16 this.context=context;17 inflater=LayoutInflater.from(context);18 this.mdata=mdata;19 }20 @Override21 public int getCount() {22 return mdata.size();23 }24 25 @Override26 public T getItem(int position) {27 return mdata.get(position);28 }29 30 @Override31 public long getItemId(int position) {32 return position;33 }34 @Override35 public View getView(int position, View convertView, ViewGroup parent)36 {37 ViewHolder holder=ViewHolder.get(context,convertView,parent, R.layout.item,position);38 covert(holder,getItem(position));39 return holder.getMconvertView();40 }41 public abstract void covert(ViewHolder holder, T t) ;42 43 }

 

 

 通用的Viewholder

1 package cct.commonadapter.adapterUtil;  2 import android.content.Context;  3 import android.graphics.Bitmap;  4 import android.util.SparseArray;  5 import android.view.LayoutInflater;  6 import android.view.View;  7 import android.view.ViewGroup;  8 import android.widget.ImageView;  9 import android.widget.TextView; 10  11 public  class ViewHolder { 12     private SparseArray
mViews; 13 protected int mPosition; 14 private View mConvertView; 15 private Context mContext; 16 protected int mLayoutId; 17 18 /**初始化一个ViewHolder 19 * @param context 20 * @param itemView 21 * @param parent 22 * @param position 23 */ 24 public ViewHolder(Context context, View itemView, ViewGroup parent, int position) { 25 mContext = context; 26 mConvertView = itemView; 27 mPosition = position; 28 mViews = new SparseArray
(); 29 mConvertView.setTag(this); 30 } 31 32 /**入口方法:用来判断页面是否加载过 33 * @param context 34 * @param convertView 35 * @param parent 36 * @param layoutId 37 * @param position 38 * @return 39 */ 40 public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) { 41 if (convertView == null) 42 { 43 View itemView = LayoutInflater.from(context).inflate(layoutId, parent, 44 false); 45 ViewHolder holder = new ViewHolder(context, itemView, parent, position); 46 holder.mLayoutId = layoutId; 47 return holder; 48 } else 49 { 50 ViewHolder holder = (ViewHolder) convertView.getTag(); 51 holder.mPosition = position; 52 return holder; 53 } 54 } 55 public View getMconvertView() { 56 return mConvertView; 57 } 58 59 /** 60 * 通过viewId获取View 61 * @param viewId 62 * @param
63 * @return 64 */ 65 public
T getView(int viewId){ 66 View view=mview.get(viewId); 67 if(view==null) 68 {view=mconvertView.findViewById(viewId); 69 mview.put(viewId,view); 70 } 71 return (T)view; 72 } 73 public int getLayoutId() 74 { 75 return mLayoutId; 76 } 77 78 public void updatePosition(int position) 79 { 80 mPosition = position; 81 } 82 83 public int getItemPosition() 84 { 85 return mPosition; 86 } 87 88 /**通过得到控件id,和类的属性名 89 * @param viewId 90 * @param text 91 * @return 92 */ 93 public ViewHolder setTexts(int viewId,String text){ 94 TextView tv=getView(viewId); 95 tv.setText(text); 96 return this; 97 } 98 public ViewHolder setImageBitmap(int viewId, Bitmap map){ 99 ImageView iv=getView(viewId);100 iv.setImageBitmap(map);101 return this;102 }103 public ViewHolder setImageResource(int viewId, int resId)104 {105 ImageView view = getView(viewId);106 view.setImageResource(resId);107 return this;108 }109 public ViewHolder setImageDrawable(int viewId, Drawable drawable)110 {111 ImageView view = getView(viewId);112 view.setImageDrawable(drawable);113 return this;114 }115 116 public ViewHolder setBackgroundColor(int viewId, int color)117 {118 View view = getView(viewId);119 view.setBackgroundColor(color);120 return this;121 }122 123 public ViewHolder setBackgroundRes(int viewId, int backgroundRes)124 {125 View view = getView(viewId);126 view.setBackgroundResource(backgroundRes);127 return this;128 }129 130 public ViewHolder setTextColor(int viewId, int textColor)131 {132 TextView view = getView(viewId);133 view.setTextColor(textColor);134 return this;135 }136 137 public ViewHolder setTextColorRes(int viewId, int textColorRes)138 {139 TextView view = getView(viewId);140 view.setTextColor(mContext.getResources().getColor(textColorRes));141 return this;142 }143 }

 

 

 

任意一个适配器只需要继承上面封装好的适配器

1 package cct.commonadapter.bean; 2 import android.content.Context; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.TextView; 8 import java.util.List; 9 import cct.commonadapter.R;10 import cct.commonadapter.adapterUtil.CommonAapter;11 import cct.commonadapter.adapterUtil.ViewHolder;12 import cct.commonadapter.bean.Message;13 14 public class MyCommonAdapter extends CommonAapter
{15 public MyCommonAdapter(Context context, List
mdata){16 super(context,mdata);17 }18 19 @Override20 public void covert(ViewHolder holder, Message ms) {21 22 holder.setTexts(R.id.mtitle,ms.getTitle())23 .setTexts(R.id.mdesc,ms.getDesc())24 .setTexts(R.id.mtime,ms.getTime())25 .setTexts(R.id.mphone,ms.getPhone());26 }27 }

 

转载于:https://www.cnblogs.com/cct1314520/p/6510127.html

你可能感兴趣的文章
sed 常用操作纪实
查看>>
C++复习:对C的拓展
查看>>
校外实习报告(九)
查看>>
android之android.intent.category.DEFAULT的用途和使用
查看>>
CAGradientLayer 透明渐变注意地方(原创)
查看>>
织梦DEDE多选项筛选_联动筛选功能的实现_二次开发
查看>>
iOS关于RunLoop和Timer
查看>>
SQL处理层次型数据的策略对比:Adjacency list vs. nested sets: MySQL【转载】
查看>>
已存在同名的数据库,或指定的文件无法打开或位于 UNC 共享目录中。
查看>>
MySQL的随机数函数rand()的使用技巧
查看>>
thymeleaf+bootstrap,onclick传参实现模态框中遇到的错误
查看>>
python字符串实战
查看>>
wyh的物品(二分)
查看>>
12: xlrd 处理Excel文件
查看>>
综合练习:词频统计
查看>>
中文url编码乱码问题归纳整理一
查看>>
Cesium应用篇:3控件(3)SelectionIndicator& InfoBox
查看>>
58. Length of Last Word(js)
查看>>
前端面试题汇总(持续更新...)
查看>>
如何成为F1车手?
查看>>