自定义列表单击复选框 [英] Custom list clicking with checkboxes
问题描述
我已经使用 SimpleCursorAdapter
从 Cursor
填充了一个 ListActivity
,当单击其中一个列表项时,它会启动另一个活动.我还使用 ViewBinder
对数据进行一些自定义转换.
I've populated a ListActivity
from a Cursor
using SimpleCursorAdapter
that starts another activity when one of the list items have been clicked. I'm also using ViewBinder
to do some custom transformation of the data.
我想向列表中的每一行添加一个 CheckBox
,因此我更改了视图并添加了一个重力正确的 CheckBox
.
I want to add a CheckBox
to each row in the list so I've changed the view and added a CheckBox
with gravity right.
添加 CheckBox
删除了点击项目的能力.当您按下列表项时,不再调用我在 ListActivity
中覆盖的 onListItemClick
方法.删除 CheckBox
解决了这个问题.这是为什么?
Adding the CheckBox
has removed the ability to click on the items. The onListItemClick
method I was overriding in ListActivity
is no longer called when you press on a list item. Removing the CheckBox
fixes this. Why is this?
此外,如果单击列表项的主要部分,我如何设置列表以便它继续执行我需要的功能,但在选中项中的 CheckBox
时具有附加功能?设置 onCheckedChangedListener
是否有效,或者是否为列表中的每个项目重用了相同的视图实例?
Also, how can I set up the list so that it continues to perform my required functionality if the main part of the list item is clicked but have additional functionality when the CheckBox
in the item is checked? Will setting a onCheckedChangedListener
work or is the same view instance reused for each item in the list?
推荐答案
看起来 SimpleCursorAdapter
对于我想要实现的目标来说太原始了.我已经切换到实现 CursorAdapter
并在我的 newView
方法实现中使用 LayoutInflater
返回一个新视图.
Looks like SimpleCursorAdapter
is too primitive for what I wanted to achieve.
I've switched to implementing CursorAdapter
and returning a new view using the LayoutInflater
in my implementation of the newView
method.
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.alarm_row, parent, false);
}
在 bindView 中,我将自定义 OnClickListener
设置为我的主要 LinearLayout
,然后将另一个 OnCheckedChangeListener
设置为 CheckBox
.
In bindView I then set a custom OnClickListener
to my main LinearLayout
and then another OnCheckedChangeListener
to the CheckBox
.
为了让这一切看起来正确,我必须将 LinearLayout
的背景设置为 android 的 menuitem drawable:
For all this to look right I had to set the LinearLayout
's background to android's menuitem drawable:
android:background="@android:drawable/menuitem_background"
这篇关于自定义列表单击复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!