以下代码是为 GalleryView/GridView 等扩展 BaseAdapter 的典型适配器:
public class GalleryImageAdapter extends BaseAdapter
{
private Context mContext;
private Integer[] ImageIds = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3
};
public GalleryImageAdapter(Context context)
{
mContext = context;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int index, View view, ViewGroup viewGroup)
{
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(ImageIds[index]);
i.setLayoutParams(new Gallery.LayoutParams(200, 200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
}
如果我们访问 Android 网站 developer.android.com,我们会发现以下抽象方法。
Adapter>BaseAdapter(适配器的子类)
abstract int getCount()
abstract Object getItem(int position)
abstract long getItemId(int position)
abstract int getItemViewType(int position)
abstract View getView(int position, View convertView, ViewGroup parent)
abstract int getViewTypeCount()
abstract boolean hasStableIds()
abstract boolean isEmpty()
abstract void registerDataSetObserver(DataSetObserver observer)
abstract void unregisterDataSetObserver(DataSetObserver observer)
我的问题是我们是否需要实现上述所有抽象方法?在我上面的示例代码中,我们只使用 getCount()、getItem()、getItemId 和 getView。为什么?