具有自定义视图的多项选择列表?
我已经看到来自 ApiDemos 的示例 com.example.android.apis.view.List11 。在该示例中,每一行都采用视图 android.R.simple_list_item_multiple_choice。每个此类视图都有一个 TextView
和一个 CheckBox
。
现在我希望每个视图都有 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示例中,我只需要单击项目行。
那么我需要做什么来为每行创建一个带有自定义视图的多项选择列表呢?非常感谢。
I've seen example com.example.android.apis.view.List11 from ApiDemos. In that example, each row takes the view android.R.simple_list_item_multiple_choice. Each such view has a TextView
and a CheckBox
.
Now I want each view to have 2 TextView
s and 1 CheckBox
, somewhat similar to the List3 example. I tried creating a custom layout file row.xml like this:
<?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>
Then in Activity
's onCreate()
, I do like this:
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);
}
The result kind of looks like what I want, but it looks like the list doesn't know which item of it is selected. Also, I need to click exactly on the CheckBox
. In the List11 example, I only need to click on the item row.
So what do I need to do to make a multiple choice list with my custom view for each row? Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您必须创建自己的
RelativeLayout
来实现Checkable
接口,并引用CheckBox
或CheckedTextView
> (如果是多项选择模式,则为列表)。看这个帖子:
http://www.marvinlabs.com/2010/ 10/29/custom-listview-ability-check-items/
You have to make your own
RelativeLayout
that implements theCheckable
interface and have a reference to theCheckBox
or to theCheckedTextView
(or a list if it's multiple choice mode).Look at this post:
http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/
如果您希望根据模型数据检查某些行,那么第一次加载列表时,Rahul Garg 的答案很好,但之后您必须自己处理检查/取消检查事件。
您可以覆盖
ListActivity
的onListItemCLick()
来检查/取消选中行如果这样做,请勿将
ListView
设置为CHOICE_MODE_MULTIPLE
,因为调用函数时会产生奇怪的结果。要检索已检查行的列表,您必须自己实现一个方法,在
ListView
上调用getCheckItemIds()
不起作用:The answer of Rahul Garg is good for the first time the list is loaded, if you want some rows to be checked depending on the model data, but after that you have to handle the check/uncheck events by yourself.
You can override the
onListItemCLick()
of theListActivity
to check/uncheck the rowsIf you do so, do not set the
ListView
toCHOICE_MODE_MULTIPLE
, because it makes strange things when calling the function.To retrieve the list of checked rows, you have to implement a method yourself, calling
getCheckItemIds()
on theListView
does not work:不,事实并非如此。它有一个
CheckedTextView
。尝试将
CheckBox
android:id
值设置为"@android:id/text1"
并查看是否有帮助。这是 Android 在simple_list_item_multiple_choice
中用于CheckedTextView
的 ID。No, it doesn't. It has a
CheckedTextView
.Try making the
CheckBox
android:id
value be"@android:id/text1"
and see if that helps. That is the ID used by Android for theCheckedTextView
insimple_list_item_multiple_choice
.解决方案是创建一个实现 Clickable 接口的自定义 View。
并使用新的小部件为列表项创建自定义布局。
然后使用上面的布局创建一个新的自定义适配器。
The solution is to create a custom View that implements the Clickable interface.
And create a custom layout for the list items using the new widget.
Then create a new custom Adapter using the layout above.
现在可以通过
的 ListActivtyClass 方法中的
自定义适配器中
一些技巧,这样您可以在列表中选择项目时自定义适配器中的视图。
It is possible by some trick
in your ListActivtyClass in method
now in you custom Adapter
this way you can customize the view in adapter when the item is selected in the list.
如何让自定义布局用作自定义复选框的简单示例:
Simple example how to get a custom layout to work as custom checkbox:
我发现这个小代码非常有用: http://alvinalexander.com/java/jwarehouse/apps-for-android/RingsExtended/src/com/example/android/rings_extended/CheckableRelativeLayout.java.shtml
这是一个很好的补充到 @ferdy182 的 http://www.marvinlabs .com/2010/10/29/custom-listview-ability-check-items/ 内容。
I found it very useful this little code: http://alvinalexander.com/java/jwarehouse/apps-for-android/RingsExtended/src/com/example/android/rings_extended/CheckableRelativeLayout.java.shtml
It is a great addition to @ferdy182 's http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/ content.
找到了解决方案...您可以通过在适配器本身中向每个视图添加侦听器来获得对视图的点击(例如自定义行布局中的复选框),同时在 getView() 中返回转换后的视图。如果您打算获取任何列表特定信息,您可能必须传递列表对象的引用。就像行号一样。
Got the solution ... You can get the clicks on the views (like checkboxes in custom layouts of row) by adding listener to each of them in the adapter itself while you return the converted view in getView(). You may possibly have to pass a reference of list object if you intent to get any list specific info. like row id.
我想确认 Pritam 的答案是正确的。您需要在每个列表项上有一个
onClickListener
(在适配器的getView()
中定义它)。您可以为每个项目创建一个新的
onClickListener()
,或者让适配器实现onClickListener()
- 在这种情况下,必须标记项目以便侦听器知道哪些项目它正在操作的项目。依赖列表
onItemClickListener()
- 正如有人在另一个线程中建议的那样 - 将不起作用,因为CheckBox
将拦截单击事件,因此列表将无法获取它。最后@Rahul 和JVitella:
情况是列表项上的
CheckBox
必须独立于列表项本身可单击和检查。因此解决方案就像我上面描述的那样。I want to confirm that the Pritam's answer is correct. You need an
onClickListener
on each list's item (define it in the adapter'sgetView()
).You can create a new
onClickListener()
for each item, or have the adapter implementonClickListener()
- in this case the items must be tagged for the listener to know, which item it is operating on.Relying on the list
onItemClickListener()
- as someone advised in another thread - will not work as theCheckBox
will intercept the click event so the list will not get it.And finally @Rahul and JVitella:
The situation is that the
CheckBox
on a list item must be clickable and checkable independently from the list item itself. Therefore the solution is as I just described above.