千家信息网最后更新 2024年11月14日Android如何自定义实现BaseAdapter
这篇文章给大家分享的是有关Android如何自定义实现BaseAdapter的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
实现原理是这样的,每次在setData中都要查找控件,然后setXXX()什么的,我们可以把这写实现放如到ViewHolder中去,在ViewHolder中写入一个链式的方法,来帮助我们来实现功能,链式方法如下:
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"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!