在上一篇中,我们说过,在setData中如果有很多控件的话,我们还是要在该方法中写入很多代码,为了降低开发的方便性,本次就在此基础上再一次优化。实现原理是这样的,每次在setData中都要查找控件,然后setXXX()什么的,我们可以把这写实现放如到VIEwHolder中去,在VIEwHolder中写入一个链式的方法,来帮助我们来实现功能(关于VIEwHodler类的代码我就不再重复写了,代码在此:AndroID之自定义实现BaseAdapter(通用适配器一) ),链式方法如下:
public VIEwHolder setText(int vIEwID,String data){ TextVIEw tv = getVIEw(vIEwID); tv.setText(data); return this; }
通过这个方法,我们可以很好的去设置对应的内容,只需要传一个需要设置控件的ID,然后传对应的数据,就可以达到设置文字的效果了,下面再看看我们的MyAdapter.java中的代码
MyAdapter.java
/*** 上一篇中的MyAdapter*/public class MyAdapter extends MyBaseAdapter { public MyAdapter(List<Student> data) { super(data); } @OverrIDe public voID setData(VIEwHolder holder,Student t) { TextVIEw tvname = holder.getVIEw(R.ID.mTv1); tvname.setText(t.getname()); TextVIEw tvSex = holder.getVIEw(R.ID.mTv2); tvSex.setText(t.getSex()); }}/*** 优化后的MyAdapter*/public class MyAdapter extends MyBaseAdapter { public MyAdapter(List<Student> data) { super(data); } @OverrIDe public voID setData(VIEwHolder holder,Student t) { holder.setText(R.ID.mTv1,t.getname()).setText(R.ID.mTv2,t.getSex()); }}
ok,对比一下setData中的实现,我们这里只用一条代码就可以代替上面的实现了,这样是不是更方便呢,这样的话,我们的扩展就更加方便了,如果说,我们不仅仅只是设置文本内容,而是通过图片控件设置图片呢?很简单,我们只需要在VIEwHolder中再添加我们要实现的方法即可,比如设置图片,我们可以添加如下代码:
public VIEwHolder setimageResource(int vIEwID,int resID){ ImageVIEw img = getVIEw(vIEwID); img.setimageResource(resID); return this; } public VIEwHolder setimageBitmap(int vIEwID,Bitmap bm){ ImageVIEw img = getVIEw(vIEwID); img.setimageBitmap(bm); return this; }
添加这两个方法后,需要设置图片就很轻松了,只需在setData中传递对应的参数即可
@OverrIDe public voID setData(VIEwHolder holder,t.getSex()); holder.setimageResource(R.ID.img1,资源ID).setimageBitmap(R.ID.img2,bm); }
好了,一个通用的适配器就已经完成了,整体框架和代码都已经实现了,我将所有的代码都综合一下,写在下面,方便大家使用,分别是MainActivity.java(主界面类,负责传参设置ListVIEw的数据)、MyAdapter.java(自定义的适配器)、MyBaseAdapter.java(通用的适配器类)、VIEwHolder.java(通用的持有类对象)、以及实体类Student.java
MainActivity.java
public class MainActivity extends AppCompatActivity{ private List<Student> data; private ListVIEw mList; MyAdapter adapter; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); getData(); mList = (ListVIEw) findVIEwByID(R.ID.mList); adapter = new MyAdapter(data); mList.setAdapter(adapter); } private voID getData() { data = new ArrayList<>(); Student stu = null; for (int i = 0; i < 20; i++) { stu = new Student(); stu.setname("姓名" + i); stu.setSex(i % 2 == 0 ? "男" : "女"); data.add(stu); } }}
MyAdapter.java
public class MyAdapter extends MyBaseAdapter<Student> { public MyAdapter(List<Student> data) { super(data); } @OverrIDe public voID setData(VIEwHolder holder,t.getSex()); }}
MyBaseAdapter.java
public abstract class MyBaseAdapter<T> extends BaseAdapter { protected List<T> data; public MyBaseAdapter(List<T> data){ this.data = data; } @OverrIDe public int getCount() { return data == null ? 0 : data.size(); } @OverrIDe public Object getItem(int position) { return data.get(position); } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEwHolder holder = VIEwHolder.getHolder(convertVIEw,parent,position,R.layout.List_item); setData(holder,data.get(position)); return holder.getConvertVIEw(); } public abstract voID setData(VIEwHolder holder,T t);}
VIEwHolder.java
public class VIEwHolder { private int position; private SparseArray<VIEw> array; private VIEw convertVIEw; private Context context; private VIEwHolder(VIEwGroup parent,int position,int layout) { this.position = position; this.context = parent.getContext(); convertVIEw = LayoutInflater.from(parent.getContext()).inflate(layout,null); convertVIEw.setTag(this); array = new SparseArray<>(); } public static VIEwHolder getHolder(VIEw convertVIEw,VIEwGroup parent,int layout) { if (convertVIEw == null) { return new VIEwHolder(parent,layout); } else { VIEwHolder holder = (VIEwHolder) convertVIEw.getTag(); holder.position = position; return holder; } } public <T extends VIEw> T getVIEw(int vIEwID) { VIEw vIEw = array.get(vIEwID); if (vIEw == null) { vIEw = convertVIEw.findVIEwByID(vIEwID); array.put(vIEwID,vIEw); } return (T) vIEw; } public VIEw getConvertVIEw() { return convertVIEw; } public VIEwHolder setText(int vIEwID,String data) { TextVIEw tv = getVIEw(vIEwID); tv.setText(data); return this; } public VIEwHolder setimageResource(int vIEwID,int resID) { ImageVIEw img = getVIEw(vIEwID); img.setimageResource(resID); return this; } public VIEwHolder setimageBitmap(int vIEwID,Bitmap bm) { ImageVIEw img = getVIEw(vIEwID); img.setimageBitmap(bm); return this; }}
Student.java
public class Student { private String name; private String sex; public String getname() { return name; } public voID setname(String name) { this.name = name; } public String getSex() { return sex; } public voID setSex(String sex) { this.sex = sex; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的Android之自定义实现BaseAdapter(通用适配器三)全部内容,希望文章能够帮你解决Android之自定义实现BaseAdapter(通用适配器三)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)