【问题标题】:android ExpandableListView add button setonchildclicklistener not workingandroid ExpandableListView 添加按钮 setonchildclicklistener 不起作用
【发布时间】:2013-10-29 03:01:21
【问题描述】:

以下代码

 <TextView
    android:id="@+id/lblListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:paddingRight="20dp"
    android:textSize="18sp" 
    android:singleLine="true" />

<TextView
    android:id="@+id/lblListDesc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:paddingTop="25dp"
    android:textSize="12sp"
    android:text="This is Desc" />    

过程 setOnGroupClickListener 的工作原理

    expListView.setOnGroupClickListener(new OnGroupClickListener() {
         @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            Toast.makeText(getApplicationContext(), "Group Clicked " + listDataHeader.get(groupPosition), Toast.LENGTH_SHORT).show();
            return true;
        }
    });

但是,如果你添加一个按钮

 <TextView
    android:id="@+id/lblListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:paddingRight="20dp"
    android:textSize="18sp" 
    android:singleLine="true" />

<Button
    android:id="@+id/iconShowParamGroup"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"            
    android:background="@drawable/ic_launcher" 
    android:onClick="showParamGroup" />

<TextView
    android:id="@+id/lblListDesc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:paddingTop="25dp"
    android:textSize="12sp"
    android:text="This is Desc" /> 

该过程 setOnGroupClickListener 不起作用。 并据此产生以下不予披露。 你能指出我的错误吗?

谢谢!

【问题讨论】:

标签: android listview button


【解决方案1】:

看起来像您添加到项目中的 Button 获得项目焦点和 onClick 事件,只需将 Button focusable 设置为 false。检查这个:

<Button
        android:id="@+id/iconShowParamGroup"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher"
        android:focusable="false"
        android:onClick="showParamGroup" />

希望这会有所帮助。

【讨论】:

  • 但是ExpandableListView列表并没有公开。什么会干扰?
  • 此解决方案不适用于 Android 6.0.1。我有一个带有 focusable = false 的 ToggleButton,这个按钮获得了所有的点击。
【解决方案2】:

您必须在 ExpandableListView 中包含参数 android:descendantFocusability="blocksDescendants"

<ExpandableListView
    android:id="@+id/treatments_expandable_list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:groupIndicator="@null"/>

【讨论】:

  • 从那以后四年过去了,我已经学到了很多东西。不过谢谢你的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多