[英]Custom Listview with checkbox not clickable
I have created a custom listview with checkbox, the items on the listview are not clickable.我创建了一个带有复选框的自定义列表视图,列表视图上的项目不可点击。 what am i doing wrong here, i want to be able to click the items on the list not just the checkbox.
我在这里做错了什么,我希望能够单击列表中的项目而不仅仅是复选框。
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
data = store.getText().toString();
list.add(data);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view, R.id.textView1, list);
ls.setAdapter(adapter);
}
});
ls.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String click = list.get(arg2).toString();
Toast.makeText(getBaseContext(), "You Clicked " + click, Toast.LENGTH_SHORT).show();
}
});
my code for list_view.xml
我的
list_view.xml
代码
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:text="" />
my activity_main.xml
file我的
activity_main.xml
文件
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1" >
</ListView>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/editText1"
android:text="Save" />
It doesnt work because adding CheckBox
to ListView
steals the focus from ListView
and you are not able click the list item .它不工作,因为加入
CheckBox
来ListView
抢断焦点ListView
,你是不是能够点击列表项。 There is a Workaround , do not use checkbox but instead of it you can use for example Drawable , ImageView or TextView with parameter setClickable(true)
or XML android:clickable=”true”
.有一个解决方法,不要使用复选框,但您可以使用 Drawable、ImageView 或 TextView 和参数
setClickable(true)
或 XML android:clickable=”true”
代替它。 It'll do the thing because you will not lose the focus of the ListView
它会做这件事,因为你不会失去
ListView
的焦点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.