是的,这是可以做到的。我已经这样做了几次,我的方法是通过扩展 android.widget.BaseAdapter 为列表创建一个自定义适配器。这可能超出了您解决问题所需的范围,但希望这能为您提供所需的内容。
这是主要的 Activity 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip"
android:background="@drawable/backrepeat"
>
<Button
android:id="@+id/cat_done_button"
android:layout_width="75dip"
android:layout_height="50dip"
android:layout_marginTop="10dip"
android:layout_marginLeft="6dip"
android:text="@string/done"
/>
<EditText
android:id="@+id/category_entry"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:hint="@string/category_hint"
android:layout_margin="10dip"
android:layout_below="@+id/cat_done_button"
/>
<ImageButton
android:id="@+id/category_add_button"
android:background="#00000000"
android:src="@drawable/ic_menu_add_ld"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="5dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignBottom="@+id/category_entry"
android:layout_toRightOf="@+id/category_entry"
/>
<ListView
android:id="@+id/category_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginBottom="5dip"
android:background="#00000000"
android:layout_below="@+id/category_notification"
/>
</RelativeLayout>
以下是列表中每行的单独布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip"
android:background="#00000000"
>
<TextView
android:id="@+id/category_name"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="10dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignParentLeft="true"
android:background="#00000000"
/>
<ImageButton
android:id="@+id/category_delete_button"
android:background="#00000000"
android:src="@drawable/ic_menu_delete_ld"
android:layout_width="50dip"
android:layout_height="50dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignParentRight="true"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
以下是主活动(Category Activity)如何设置其 List 对象:
private void setUpCategoryList() {
categoryList = (ListView) this.findViewById(R.id.category_list);
categoryAdapter = new CategoryAdapter(this, android.R.layout.simple_list_item_1,
categoryManager.getCategoriesList());
categoryList.setAdapter(categoryAdapter);
}
这是自定义的 CategoryAdapter:
public class CategoryAdapter extends BaseAdapter implements OnClickListener {
private CategoryActivity catActivity;
private List<String> categoryList;
private final String CAT_DELETED = "Category has been deleted.";
private final String DEFAULT_DELETE = "Cannot delete Default category.";
private final String NON_EMPTY_DELETE = "Category has entries. Entries re-assigned to Default category.";
public CategoryAdapter(CategoryActivity catActivity, int resource, List<String> items) {
this.catActivity = catActivity;
categoryList = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String category = categoryList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) catActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_row_layout, null);
}
TextView categoryView = (TextView) convertView.findViewById(R.id.category_name);
categoryView.setText(category);
ImageButton deleteButton = (ImageButton) convertView.findViewById(R.id.category_delete_button);
deleteButton.setOnClickListener(this);
deleteButton.setTag(category);
return convertView;
}
@Override
public void onClick(View view) {
//Do stuff here
}