简体   繁体   English

在 ListView 中单击复选框

[英]CheckBox click in ListView

I have a listview with some text and a checkbox.我有一个带有一些文本和复选框的列表视图。 I can click the checkbox, and the check changes, but when I try to listen for the check being changed, or the click, the listener never triggers.我可以单击复选框,然后检查更改,但是当我尝试侦听更改的检查或单击时,侦听器永远不会触发。

I have the listview in an Activity (not a ListActivity)我在活动中有列表视图(不是 ListActivity)

schedule.xml layout: schedule.xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/schedulerLayout">
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingBottom="10dp">
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/currentHours"
                android:textSize="50dp"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="50dp"
                android:text=":"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/currentMinutes"
                android:textSize="50dp"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="vertical">
        <!--vertical filler for am/pm display -->
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ampm"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:orientation="vertical">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/currentDayOfWeek"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/currentDate"/>
    </LinearLayout>
</LinearLayout>

<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scheduleButton"
        android:text="@string/newSchedule"/>
<View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#FFFFFFFF"/>

<ListView
        android:id="@+id/scheduler_list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</LinearLayout>

schedulerow.xml: schedulerow.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true">
<TextView
        android:id="@+id/scheduleListTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:text="Title"/>

<TextView
        android:id="@+id/scheduleListDays"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/scheduleListTitle"
        android:text="Days"/>
<TextView
        android:id="@+id/scheduleListMonths"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/scheduleListDays"
        android:text="Months"/>
<View
        android:id="@+id/schedulerSeparator"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#FFFFFFFF"
        android:layout_below="@id/scheduleListMonths"/>
<CheckBox
        android:id="@+id/scheduleCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="true"
        android:layout_above="@id/schedulerSeparator"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>
<TextView
        android:id="@+id/scheduleListTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/scheduleCheckBox"
        android:text="Time"/>
</RelativeLayout>

I'm filling in the data like this:我正在填写这样的数据:

SimpleAdapter adapter = new SimpleAdapter(this,
            data,
            R.layout.schedulerow,
            new String[] {"Title","Time","Day", "Month"},
            new int[] { R.id.scheduleListTitle, R.id.scheduleListTime,R.id.scheduleListDays, R.id.scheduleListMonths});

listView.setAdapter(adapter);

If I try to add a listener to the checkbox, it does nothing.如果我尝试向复选框添加侦听器,则它什么也不做。 Any suggestions?有什么建议? I've looked through a TON of sample code and similar questions, and none of it seems to work.我已经查看了大量示例代码和类似问题,但似乎都不起作用。

One solution is to forget the list item click listener and use another listener for the rest of the item other than the checkbox.. In item layout xml, put the other views in single layout and set the click listener on this and set the check listener on checkbox as usual.一种解决方案是忘记列表项单击侦听器并为复选框以外的其余项使用另一个侦听器.. 在项目布局 xml 中,将其他视图放在单个布局中并在此设置单击侦听器并设置检查侦听器像往常一样在复选框上。 Both will work without issues.两者都可以正常工作。 Dont use the list item click listener.不要使用列表项单击侦听器。

class MySimpleAdapter extends SimpleAdapter {
 ... 
   @Override
   void getView(.....) {
       View view = super.getView(.....);
       view.findViewById(R.id.checkboxid).setOnCheckChangedListner(checklistener);
       view.findViewById(R.id.restOfitemview).setOnClickListner(clickListner);
       return view;
    }
}

Define the two listeners and set as shown above.定义两个监听器并设置如上图所示。

Edit : First try your method(blocking focus on checkbox) with the MySimpleAdapter setting only the checkchangelistener.. If that does not work then use above solution of dividing the item layout.编辑:首先尝试使用MySimpleAdapter仅设置 checkchangelistener 的方法(阻止焦点在复选框上)。如果这不起作用,则使用上述划分项目布局的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM