android - 单击图标 ExpandableListView

标签 android expandablelistview expandablelistadapter

我有一个扩展 BaseExpandableListAdapter 的类 MyAdapter。 我想要一个在 ConstraintLayout 中带有 TextViewImageButton 的项目。

函数:

 public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = (ConstraintLayout) activity.getLayoutInflater().inflate(R.layout.constraint, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tv_mytext);
        ImageButton imgBtn = (ImageButton) convertView.findViewById(R.id.img_btn_my);
        if (tv != null) tv.setText("MyText");
        if (imgBtn != null) imgBtn.setOnClickListener(.....);
        return convertView;
    }

布局:

<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:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tv_mytext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginTop="2dp"
        android:background="@drawable/selector_item_black"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:textAppearance="?android:attr/textAppearanceMedium"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/img_btn_my"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_expand_more" />

</android.support.constraint.ConstraintLayout>

最好的方法是:

  • 在一个项目上有一个点击监听器
  • 仅在 imgButton 上有一个点击监听器

如果我在 imgButton 上添加点击,项目点击监听器将不起作用。

最佳答案

要在 imgButton 上添加点击后让项目可点击,请将其声明到 ConstraintLayout

android:descendantFocusability="blocksDescendants" 

关于android - 单击图标 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49126789/

相关文章:

android - 在布局中添加 ExpandableListView

android - 知道列表滚动何时到达底部

android - build.gradle 中的未知属性 'LibraryVariants'

android:更改用于导航的可扩展 ListView 的指示器图标和大小

java - 隐藏没有 child 的组 - ExpandableListView

android - 可扩展 ListView 中组中的不同元素

android - 使用带有复选框的 ExpandableListActivity

android - 在 Android 的应用程序管理器中隐藏应用程序

android - 在android中使用asmack获取错误请求400错误

android - ExpandableListView onExpand 总是将展开的组滚动到顶部