成都网站建设设计

将想法与焦点和您一起共享

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

在上一篇中,我们说过,在setData中如果有很多控件的话,我们还是要在该方法中写入很多代码,为了降低开发的方便性,本次就在此基础上再一次优化。实现原理是这样的,每次在setData中都要查找控件,然后setXXX()什么的,我们可以把这写实现放如到ViewHolder中去,在ViewHolder中写入一个链式的方法,来帮助我们来实现功能(关于ViewHodler类的代码我就不再重复写了,代码在此:Android之自定义实现BaseAdapter(通用适配器一) ),链式方法如下:

成都网站建设、做网站介绍好的网站是理念、设计和技术的结合。创新互联拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。

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 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 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 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 {

 public MyAdapter(List 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 extends BaseAdapter {
 protected List data;
 public MyBaseAdapter(List 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 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 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(通用适配器三)
本文路径:http://chengdu.cdxwcx.cn/article/gicccj.html