Android CheckBox和Row-OnClickListener

时间:2016-09-07 22:16:32

标签: android checkbox listener

将CheckBox添加到listView-row会停用该行本身的onClickListener!我想要有行,您可以单击行本身并选中复选框。怎么做?

//主要活动中的行侦听器

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           //Does not get called
     }
});

//适配器中的Checkbox-listener

  final CheckBox checkBox = (CheckBox) customView.findViewById(R.id.checkbox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checked.set(position, checkBox.isChecked());
            }
        });

//行-布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_marginLeft="@dimen/custom_margin_left_and_right"
    android:layout_marginStart="@dimen/custom_margin_left_and_right"
    android:layout_marginRight="@dimen/custom_margin_left_and_right"
    android:layout_marginEnd="@dimen/custom_margin_left_and_right">

    <TextView
        android:id="@+id/nameTv"
        android:layout_weight="0.9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/unbenannt"
        android:textColor="@color/grey"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_weight="0.1"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"/>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

添加

机器人:descendantFocusability =&#34; blocksDescendants&#34;

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:descendantFocusability="blocksDescendants"
        android:layout_marginLeft="@dimen/custom_margin_left_and_right"
        android:layout_marginStart="@dimen/custom_margin_left_and_right"
        android:layout_marginRight="@dimen/custom_margin_left_and_right"
        android:layout_marginEnd="@dimen/custom_margin_left_and_right">

        <TextView
            android:id="@+id/nameTv"
            android:layout_weight="0.9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/unbenannt"
            android:textColor="@color/grey"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginEnd="16dp"
            />

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_weight="0.1"
            android:layout_gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:layout_marginEnd="16dp"/>

    </LinearLayout>

答案 1 :(得分:1)

将这两个属性添加到xml中的复选框:

<CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="CheckBox" />

示例:

<a href="{%= view_path %}">Go!</a>

答案 2 :(得分:0)

进行以下更改

<CheckBox
        android:id="@+id/checkbox"
        android:layout_weight="0.1"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:focusable="false" 
        android:clickable="true"  
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"/>

您需要将复选框的焦点设置为false。然后你将开始单击listview的内部行。在您的情况下,复选框正在获得焦点,因此可以防止单击列表视图行。如果不随意评论我的问题,这应该有用。