提问人:Sarah Multitasker 提问时间:7/31/2023 最后编辑:Sarah Multitasker 更新时间:7/31/2023 访问量:31
Onclicklistener 在稍后添加以查看的部分内容中不起作用
Onclicklistener not working in part that get's added to view later
问:
我在布局上有一个 onclicklistener,其中包含两个包含。 其中一个包含一个约束布局,该布局将在单击容器时可见。 如果我再次单击布局,它也应该再次不可见。 如果我单击始终可见的部分,它就会起作用。但是,如果我单击新添加的约束布局区域,则不会发生任何反应。 onclick-listener 不会触发。 不过,它适用于包含周围的任何地方。例如,单击它的左侧(左侧有原始边距),它会再次关闭。 以下是(简化的)布局:
<?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 没有根据布局更改调整点击区域,但由于我可以通过单击左侧和下方的包含部分来触发侦听器,它一定是其他东西。
recyclerview 中使用的项目也设置为 clickable = “false”:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
</data>
<TextView
android:id="@+id/detail_text"
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Text.SecondaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"/>
</layout>
答:
1赞
Sarah Multitasker
7/31/2023
#1
显然,这是 recyclerviews 的一个已知问题,它们捕获了点击事件,因为它们将其用于滚动。 可以在此处找到许多解决方案: 单击 recyclerview 时未触发父点击事件 就我而言,使 recyclerview 不可触及是一种合理的方法,因为所有元素都已经在另一个 recyclerview 中,因此它们永远不会被砍掉,而是填充所需的空间。
评论
ConstraintView
android:clickable="false"
RecyclerView
RecyclerView
RecyclerView