理解adapterview

来源:互联网 发布:2017qq协议源码 编辑:程序博客网 时间:2024/04/29 00:47

Adapterview2个作用,一个是将数据绑定到view上,还有一个是对选择的数据进行处理。今天主要讲第一个作用。要理解adapterview,我们首先需要理解adapter设计模式。看设计模式精解里面引用的四人团解释:将一个类的接口转换成客户希望的另外一个接口。Adapter模式使原本由于接口不兼容而不能一起工作的那些类可以一起工作。举个小例子:
/**

@description
TODO
@since 2011-2-9

@version 0.1

*/
public
class DoorAdapter implements Door{

private OldDoor od = new OldDoor();

private UsualDoor ud = new UsualDoor();

private ModernDoor md = new ModernDoor();


public
void open(){

od.stickIn();

ud.lock();

md.fingerIn();

};

public
void close(){

od.StickOut();

ud.unlock();

md.fingerOut();

};
}
interface Door{

void open();

void close();
}
//老式门只能用木棍撑住来实现锁门开门
class OldDoor{

void stickIn(){};

void StickOut(){};
}
//现在常见的门有锁
class UsualDoor{

void lock(){};

void unlock(){};
}
//现代门有密码或生物指纹等
class ModernDoor{

void fingerIn(){};

void fingerOut(){};
}
很简单吧,doorAdapter将各式各样的开关门的方法适配到Door接口的open和close方法,这样我们只需要关心
Door接口就可以开关所有门了。
理解了adapter,我们就可以理解android中的adapterView了。在adapter中,有如下一些函数,只要实现了这些函数的特定功能,android就可以将将itemview组装起来,并在页面上显示。
abstract int
getCount()
How many items are in the data set represented by this Adapter.
abstract Object
getItem(int position)
Get the data item associated with the specified position in the data set.
abstract long
getItemId(int position)
Get the row id associated with the specified position in the list.
abstract int
getItemViewType(int position)
Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.
abstract View
getView(int position, View convertView, ViewGroup parent)
Get a View that displays the data at the specified position in the data set.
abstract int
getViewTypeCount()
Returns the number of types of Views that will be created by getView(int, View, ViewGroup).
abstract boolean
hasStableIds()
Indicated whether the item ids are stable across changes to the underlying data.
abstract boolean
isEmpty()
abstract void
registerDataSetObserver(DataSetObserver observer)
Register an observer that is called when changes happen to the data used by this adapter.
abstract void
unregisterDataSetObserver(DataSetObserver observer)
Unregister an observer that has previously been registered with this adapter via registerDataSetObserver(DataSetObserver).