Onclicklistener 部分不起作用,稍后添加以查看

问题描述 投票:0回答:0

我的布局上有一个 onclicklistener,其中包含两个包含。 其中一个包含内容包含约束布局,单击容器后该布局将可见。 如果我再次单击布局,它也应该再次变得不可见。 如果我单击始终可见的部分,它就会起作用。但是,如果我单击新添加的约束布局区域,则不会发生任何情况。 onclick-listener 不会触发。 但它可以在 include 周围的任何地方工作。例如。单击它的左侧(原来的左侧有一个边距)它会再次关闭。 以下是(简化的)布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto">

    <data>
      //Viewmodel
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{() -> viewModel.onClickItemContainer()}"
            android:clickable="true">

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
              >
                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:animateLayoutChanges="true">

                    <include
                        android:id="@+id/item_main"
                        layout="@layout/item_always_visible"
                        bind:viewModel="@{viewModel}"
                        bind:layout_constraintTop_toTopOf="parent"
                        bind:layout_constraintStart_toStartOf="parent"/>

                    <include
                        layout="@layout/item_details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        bind:viewModel="@{viewModel}"
                        bind:layout_constraintTop_toBottomOf="@id/item_main"
                        bind:layout_constraintStart_toStartOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

        </FrameLayout>

    </LinearLayout>

</layout>

包含的布局(打开/关闭):

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <data>
        //Viewmodel
        <import type="android.view.View" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/entry_details"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="@{viewModel.detailsVisible? View.VISIBLE : View.GONE}"
        tools:visibility="visible"
        android:clickable="false">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:adapter="@{viewModel.adapter}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:clickable="false"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

我已经在包含的布局中的几个地方尝试过使用 clickable="false" ,因为我在另一个问题中发现了该建议,但它没有任何帮助。 如果它影响整个扩展区域(也包括下面的卡片视图,例如),我会认为这是 Android 的一个错误,没有使点击区域适应布局更改,但因为我可以通过单击左侧和下面包含的部分来触发侦听器,一定是别的东西。

android layout android-animation onclicklistener
© www.soinside.com 2019 - 2024. All rights reserved.