首页 > 解决方案 > 当我使用约束布局时无法在展开的列表视图中单击子项,而使用线性布局时它可以正常工作

问题描述

我已经从堆栈的某个地方复制了扩展列表视图的代码并且它工作正常,我能够展开列表并单击父项和子项,但是当我将 check_box 元素添加到布局时,子视图单击不起作用更多的。请帮帮我,这很奇怪,因为我只是添加了一个复选框元素。

// does not work with when check box element is there
 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/expandedListItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="63dp"
            android:layout_height="19dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="56dp"
            android:layout_marginRight="56dp"
            android:text="Available"
            app:layout_constraintBottom_toBottomOf="@+id/expandedListItem"
            app:layout_constraintEnd_toEndOf="@+id/expandedListItem"
            app:layout_constraintTop_toTopOf="@+id/expandedListItem"
            app:layout_constraintVertical_bias="0.0" />

        <CheckBox
            android:id="@+id/available"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:onClick="onCheckboxClicked"
            android:text=""
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/expandedListItem"
            app:layout_constraintHorizontal_bias="0.829"
            app:layout_constraintStart_toEndOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    </android.support.constraint.ConstraintLayout>


//Here is the activity part where click methods have been written.
 expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                @Override
                public void onGroupExpand(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            expandableListTitle.get(groupPosition) + " List Expanded.",
                            Toast.LENGTH_SHORT).show();
                    Log.d("Expanded1", "Expanded");
                }
            });

            expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
                @Override
                public void onGroupCollapse(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            expandableListTitle.get(groupPosition) + " List Collapsed.",
                            Toast.LENGTH_SHORT).show();
                }
            });

            expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                                            int groupPosition, int childPosition, long id) {
                    Log.d("Child Clicked", "Child Clicked");
                    Toast.makeText(
                            getApplicationContext(),
                            expandableListTitle.get(groupPosition)
                                    + " -> "
                                    + expandableListDetail.get(`enter code here`
                                    expandableListTitle.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT
                    ).show();

                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("itemName", expandableListDetail.get(
                            expandableListTitle.get(groupPosition)).get(
                            childPosition)  ));
                    params.add(new BasicNameValuePair("available", "N"));
                    JSONParser jParser = new JSONParser();
                    jParser.hitURL(url, params);
                    Log.d("Update Stock-1", "Update");

                    return false;
                }
            });

单击子视图项应该在这两种情况下都有效,但是在存在复选框的情况下,甚至无法识别对子项的单击,甚至没有调用方法 expandableListView.setOnChildClickListener....

标签: androidandroid-linearlayoutexpandablelistviewandroid-constraintlayoutonitemclicklistener

解决方案


android:focusable="false"您需要添加CheckBox

<CheckBox
    android:id="@+id/available"
    android:focusable="false"
      ....
 />

推荐阅读