我的活动通过ListActivity
扩展ArrayAdapter
:
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, R.id.text, items);
setListAdapter(fileList);
它使用行的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkbox"
style="@style/CheckBox" />
<TextView
android:id="@+id/text"
style="@style/Label.Plain"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:gravity="center_vertical" />
</LinearLayout>
活动有onListItemClick
:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// ...
}
当我的行由一个TextView
组成时,它工作正常。但是当我将其更改为布局时,onListItemClick
停止被调用。为什么呢?
答案 0 :(得分:4)
焦点转移到CheckBox
,这样你的项目就不会获得点击事件。尝试像这样修改XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkbox"
style="@style/CheckBox"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="@+id/text"
style="@style/Label.Plain"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:gravity="center_vertical" />
</LinearLayout>
答案 1 :(得分:0)
将波纹管属性设置为复选框和textview
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
答案 2 :(得分:0)
如果你有像复选框按钮radiobutton这样的小部件,那么你必须在xml的listview元素中添加这个属性: -
android:focusableInTouchMode="true"
现在,您的点击监听器将起作用,如果您实现它,也可以使用复选框监听器。
答案 3 :(得分:0)
是的,这是常见问题总是被问到,这是因为自定义布局的子控件获得列表前的点击,因此列表不会响应点击。添加android:focusable=false;
这将允许列表听取点击。
<CheckBox
android:id="@+id/checkbox"
android:focusable="false"
style="@style/CheckBox" />
<TextView
android:id="@+id/text"
style="@style/Label.Plain"
android:focusable="false"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:gravity="center_vertical" />