如何自定义ArrayAdapter

2025-03-26 07:14:19
推荐回答(1个)
回答(1):

image_item.xml

http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

android:id="@+id/item_thumbnail"
android:layout_height="48dip"
android:layout_width="48dip"
/>

android:id="@+id/item_file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingLeft="5dip"
/>

2. 自定义ArrayAdapter
ImageListAdapter
public class ImageListAdapter extends ArrayAdapter{
private int resource;
public ImageListAdapter(Context context, int resourceId, List objects) {
super(context, resourceId, objects);
// 记录下来稍后使用
resource = resourceId;
}

public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout imageListView;
// 获取数据
File file = getItem(position);
String fileName = file.getName();
Bitmap bitmap = getBitmapFromFile(file);