【发布时间】:2010-04-16 10:17:53
【问题描述】:
我从 ApiDemos 看到了示例 com.example.android.apis.view.List11。在该示例中,每一行都采用 android.R.simple_list_item_multiple_choice 视图。每个这样的视图都有一个TextView 和一个CheckBox。
现在我希望每个视图有 2 个TextViews 和 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 示例中,我只需要单击项目行。
那么我需要怎么做才能使用我的自定义视图为每一行制作一个多项选择列表?非常感谢。
【问题讨论】:
-
你找到这个答案了吗?也许是一个例子?我有同样的问题:) stackoverflow.com/questions/3858501/…
-
那么有什么解决办法吗?我遇到了同样的问题。
标签: android listview user-interface custom-view