自定义适配器
有时候,系统提供的适配器并不能满足我们的需要,这时候我们需要自定义适配器。自定义适配器方式如下
1.新建一个适配器类继承BaseAdapter类,实现所有的abstract方法
public class PersonAdapter extends BaseAdapter {
@Override
public int getCount() {
}
@Override
public Object getItem(int position) {
}
@Override
public long getItemId(int position) {
}
@Override
public View getView(int position, View contextView, ViewGroup parent) {
}
}
2.具体实现每一个方法
(1) getCount()方法 用来得到要绑定的数据的总数 假设我们要绑定的数据是这样的
List<Person> 这里Person是一个很简单的类 具有姓名 电话 余额三个属性 另外要绑定的Item的xml文件也需要设置,我们将这两个定义为适配器的成员
//要绑定的数据
private List<Person> persons;
//绑定的条目界面
private int resource;
那么
public int getCount() {
return persons.size();
}
2.getItem(int position) 方法 外面传来一个索引值,返回索引对应的元素即为
public Object getItem(int position) {
return persons.get(position);
}
3.public long getItemId(int position) 返回条目的Id
public long getItemId(int position) {
return position;
}
4.public View getView(int position, View contextView, ViewGroup parent)方法很重要, 是取得条目的View对象的方法,这里面我们要实现数据的绑定。
【参数的说明
】
param1 position表示要绑定的数据在集合中的索引值
param2 contextView如下解释
ListView有这样的一个特性,显示第一页的时候,会将条目的View对象 new出来,显示第二页时候,它会重用第一页已经new好的对象只是将View对象所绑定的值改变,即具有缓存功能。
ListView调用getView方法时,会将之前缓存过的View对象传过来,就是param2 。这里我们也要实现缓存功能所以先判断contextView(缓存的View对象)是否为空,如果为空说明是第一次显示,那么我们便创建View对象。这里我们可以使用LayoutInflater来生成View对象。LayoutInflater是一个内置服务,。功能就是根据xml文件生成View对象
我们可以通过上下文对象取得它 LayoutInflater inflater =(LayoutInflater ) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);我们将他加入构造器中(将LayoutInflater 声明为一个成员变量)
//可以使用xml文件生成一个view对象
private LayoutInflater inflater;
public PersonAdapter(Context context,List<Person> persons,int resource) {
this.persons = persons;
this.resource = resource;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
下面进行判断创建Item的View对象
if(contextView == null){
contextView = inflater.inflate(resource, null);//生成Item界面对象
}
然后找到每个控件 对他赋值
TextView nameView = (TextView)contextView.findViewById(R.id.name);
TextView phoneView = (TextView)contextView.findViewById(R.id.phone);
TextView amountView = (TextView)contextView.findViewById(R.id.amount);
根据position取得要绑定的数据
TextView phoneView = (TextView)contextView.findViewById(R.id.phone);
TextView amountView = (TextView)contextView.findViewById(R.id.amount);
根据position取得要绑定的数据
Person person = Persons.get(position)
;
数据绑定
nameView.setText(person.getName());
phoneView .setText(person.getPhone());
amountView .setText(person.getAmount().toString());
最后返回生成的View对象
return convertView;
最后附上改进的getView的源码
public View getView(int position, View contextView, ViewGroup parent) {
TextView nameView = null;
TextView phoneView = null;
TextView amountView = null;
if(contextView == null){
contextView = inflater.inflate(resource, null);//生成Item界面对象
nameView = (TextView)contextView.findViewById(R.id.name);
phoneView = (TextView)contextView.findViewById(R.id.phone);
amountView = (TextView)contextView.findViewById(R.id.amount);
ViewCache cache = new ViewCache();
cache.nameView = nameView;
cache.phoneView = phoneView;
cache.amountView = amountView;
contextView.setTag(cache);
}else{
ViewCache cache = (ViewCache)contextView.getTag();
nameView = cache.nameView;
phoneView = cache.phoneView;
amountView = cache.amountView;
}
Person person = persons.get(position);
//下面代码实现数据绑定
nameView.setText(person.getName());
phoneView.setText(person.getPhone());
amountView.setText(person.getAmount().toString());
return contextView;
}
private final class ViewCache{
public TextView nameView;
public TextView phoneView;
public TextView amountView;
}