我从 ApiDemos 看到了示例 com.example.android.apis.view.List11。在该示例中,每一行都采用 android.R.simple_list_item_multiple_choice View 。每个这样的 View 都有一个 TextView
和一个 CheckBox
。
现在我希望每个 View 有 2 个 TextView
和 1 个 CheckBox
,有点类似于 List3 示例。我尝试像这样创建自定义布局文件 row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="@+id/checkbox"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/text_name"
android:textSize="13px"
android:textStyle="bold"
android:layout_toLeftOf="@id/checkbox"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_phone"
android:textSize="9px"
android:layout_toLeftOf="@id/checkbox"
android:layout_below="@id/text_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
然后在Activity
的onCreate()
中,我这样做:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Query the contacts
mCursor = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row,
mCursor,
new String[] { Phones.NAME, Phones.NUMBER},
new int[] { R.id.text_name, R.id.text_phone });
setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
结果有点像我想要的,但看起来列表不知道选择了哪个项目。另外,我需要准确地单击CheckBox
。在 List11 示例中,我只需要单击项目行。
那么我需要怎么做才能使用我的自定义 View 为每一行创建一个多项选择列表?非常感谢。
最佳答案
您必须制作自己的 RelativeLayout
来实现 Checkable
接口(interface),并引用 CheckBox
或 CheckedTextView
(如果是多选模式,则为列表)。
看看这个帖子: http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/
关于android - 带有自定义 View 的多项选择列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2652109/