Android中的标准适配器很容易使用,但它们具有一些限制。为了解决这些限制,Android提供了一个名为BaseAdapter的抽象类,如果需要自定义适配器,可以扩展它。如果拥有特殊的数据管理需要,或者如果希望对子视图显示方式实施更多控制,可以使用自定义适配器。也可以使用自定义适配器来通过缓存技术改进性能。接下来介绍如何构建自定义适配器。
对于这个实例,我们的适配器将处理一些海牛的图像,所以我们将它命名为ManateeAdapter。我们也将在一个活动内创建它。
自定义适配器:ManateeAdapter
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/gridviewcustom.xml -->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:numColumns="auto_fit"
android:gravity="center"
/>
public class GridViewCustomAdapter extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gridviewcustom);
GridView gv = (GridView)findViewById(R.id.gridview);
ManateeAdapter adapter = new ManateeAdapter(this);
gv.setAdapter(adapter);
}
public static class ManateeAdapter extends BaseAdapter {
private static final String TAG = "ManateeAdapter";
private static int convertViewCounter = 0;
private Context mContext;
private LayoutInflater mInflater;
static class ViewHolder {
ImageView image;
}
private int[] manatees = {
R.drawable.manatee00, R.drawable.manatee01, R.drawable.manatee02,
R.drawable.manatee03, R.drawable.manatee04, R.drawable.manatee05,
R.drawable.manatee06, R.drawable.manatee07, R.drawable.manatee08,
R.drawable.manatee09, R.drawable.manatee10, R.drawable.manatee11,
R.drawable.manatee12, R.drawable.manatee13, R.drawable.manatee14,
R.drawable.manatee15, R.drawable.manatee16, R.drawable.manatee17,
R.drawable.manatee18, R.drawable.manatee19, R.drawable.manatee20,
R.drawable.manatee21, R.drawable.manatee22, R.drawable.manatee23,
R.drawable.manatee24, R.drawable.manatee25, R.drawable.manatee26,
R.drawable.manatee27, R.drawable.manatee28, R.drawable.manatee29,
R.drawable.manatee30, R.drawable.manatee31, R.drawable.manatee32,
R.drawable.manatee33 };
private Bitmap[] manateeImages = new Bitmap[manatees.length];
private Bitmap[] manateeThumbs = new Bitmap[manatees.length];
public ManateeAdapter(Context context) {
Log.v(TAG, "Constructing ManateeAdapter");
this.mContext = context;
mInflater = LayoutInflater.from(context);
for(int i=0; i<manatees.length; i++) {
manateeImages[i] = BitmapFactory.decodeResource(
context.getResources(), manatees[i]);
manateeThumbs[i] = Bitmap.createScaledBitmap(manateeImages[i],
100, 100, false);
}
}
public int getCount() {
Log.v(TAG, "in getCount()");
return manatees.length;
}
public int getViewTypeCount() {
Log.v(TAG, "in getViewTypeCount()");
return 1;
}
public int getItemViewType(int position) {
Log.v(TAG, "in getItemViewType() for position " + position);
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Log.v(TAG, "in getView for position " + position +
", convertView is " +
((convertView == null)?"null":"being recycled"));
if (convertView == null) {
convertView = mInflater.inflate(R.layout.gridimage, null);
convertViewCounter++;
Log.v(TAG, convertViewCounter + " convertViews have been created");
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.gridImageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageBitmap(manateeImages[position]);
return convertView;
}
public Object getItem(int position) {
Log.v(TAG, "in getItem() for position " + position);
return manateeImages[position];
}
public long getItemId(int position) {
Log.v(TAG, "in getItemId() for position " + position);
return position;
}
}
}
效果图: