[英]Do we need to implement all the abstract methods in Android?
The following code is the typical adapter extends BaseAdapter for GalleryView/GridView etc.: 以下代码是BaseAdapter用于GalleryView / GridView等的典型适配器扩展:
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;
}
}
If we go to Android website, developer.android.com, we find the following abstract methods. 如果访问Android网站developer.android.com,则会发现以下抽象方法。
Adapter>BaseAdapter(subclass of 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)
My question is do we need to implement all the above abstract methods? 我的问题是我们是否需要实现所有上述抽象方法? In my example code above, we use getCount(), getItem(), getItemId and getView only.
在上面的示例代码中,我们仅使用getCount(),getItem(),getItemId和getView。 Why?
为什么?
I asked myself this same question myself this morning and then I did some investigations. 今天早上我问自己同样的问题,然后进行了一些调查。
If you look at the code for BaseAdapter class: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/BaseAdapter.java you will see that some of the abstract methods in your list are already implemented through BaseAdapter. 如果您查看BaseAdapter类的代码: http ://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/BaseAdapter.java,看到列表中的某些抽象方法已经通过BaseAdapter实现。
You therefore only need to implement the methods that are not already implemented within BaseAdapter, such as getCount() or getItem(). 因此,您仅需要实现BaseAdapter中尚未实现的方法,例如getCount()或getItem()。 Android only forces you to implement those methods that are not already implemented and you can override the methods already implemented of course.
Android仅强制您实现尚未实现的那些方法,并且您可以覆盖当然已经实现的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.