欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android之自定义实现BaseAdapter(通用适配器三)

程序员文章站 2022-03-31 14:07:50
在上一篇中,我们说过,在setdata中如果有很多控件的话,我们还是要在该方法中写入很多代码,为了降低开发的方便性,本次就在此基础上再一次优化。实现原理是这样的,每次在setdata中...

在上一篇中,我们说过,在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, student t) {
  holder.settext(r.id.mtv1, t.getname()).settext(r.id.mtv2, 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, student t) {
  holder.settext(r.id.mtv1, t.getname()).settext(r.id.mtv2, 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 position, int layout) {
  if (convertview == null) {
   return new viewholder(parent, position, 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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。