首页 > 解决方案 > 带有内部视图组的 CollapsingToolbarLayout 可防止单击其中的嵌套视图

问题描述

这是我的活动 XML 的顶部 -

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/activity_product_page_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/activity_product_page_appbarlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">


        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/activity_product_page_top_product_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <ImageView
                    android:id="@+id/activity_product_page_back_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginEnd="@dimen/marketplace_14dp"
                    android:contentDescription="@string/marketplace_productvendor_page_back_button"
                    android:onClick="backButtonPressed"
                    android:src="@drawable/arrow_left"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <ImageView
                    android:id="@+id/activity_product_page_vendor_icon_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginEnd="@dimen/marketplace_14dp"
                    android:contentDescription="@string/marketplace_productvendor_page_vendor_image"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/ic_launcher"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:layout_width="200dp" />

                <FrameLayout
                    android:id="@+id/activity_product_page_shopping_cart_framelayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/marketplace_14dp"
                    android:layout_marginEnd="@dimen/marketplace_14dp"
                    android:background="@color/white"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <ImageView
                        android:id="@+id/activity_checkout_cart_imageView"
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_margin="20dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="10dp"
                        android:src="@drawable/icons_32_x_32_black_shopping_cart" />

                    <TextView
                        android:id="@+id/activity_product_page_shopping_cart_counter"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="end"
                        android:layout_marginTop="5dp"
                        android:layout_marginEnd="3dp"
                        android:background="@drawable/textview_round_background"
                        android:elevation="1dp"
                        android:gravity="center"
                        android:maxLines="1"
                        android:textAlignment="center"
                        android:textColor="@color/white"
                        android:textSize="10sp"
                        android:textStyle="bold"
                        android:visibility="gone"
                        app:autoSizeMaxTextSize="16sp"
                        app:autoSizeMinTextSize="10sp"
                        app:autoSizeStepGranularity="2sp"
                        app:layout_constraintStart_toEndOf="@+id/activity_checkout_cart_imageView"
                        app:layout_constraintTop_toTopOf="parent"
                        tools:ignore="SmallSp"
                        tools:text="1"
                        tools:visibility="visible" />


                </FrameLayout>

                <View
                    android:id="@+id/activity_shopping_cart_top_view"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/marketplace_view_line_color"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

我现在面临的问题是我的 CollapsingToolbarLayout 内的约束布局内的所有视图都不可点击。

我已经检查了关于这个主题的多个问题,但没有一个对我有帮助 - 有些人建议创建一个自定义 Toolbar 类,它onTouchEvent()总是覆盖为 false,但是当我尝试使用这个类时,由于某种原因它没有显示在 XML 编辑器中。

我在网上找到的其他解决方案并没有为我提供任何有用的解决方案。

那么如何使嵌套视图可点击?

标签: androidonclicklistenerandroid-collapsingtoolbarlayoutandroid-viewgroup

解决方案


解决了,我将以下行添加到我的 CollapsingToolbarLayout -

android:descendantFocusability="blocksDescendants"

推荐阅读