Onclicklistener在后来添加到视图的部分不起作用。

huangapple go评论116阅读模式
英文:

Onclicklistener not working in part that get's added to view later

问题

我在一个布局上有一个onclicklistener,其中包含两个include。其中一个include包含一个约束布局,在容器点击时将显示。如果我再次点击布局,它也应该变为不可见。如果我点击一直可见的部分,它可以正常工作。但是,如果我点击新添加的约束布局区域,什么都不会发生。onclick监听器不会触发。但是,如果我在include周围的任何地方点击,例如在左侧点击(原始布局中有一个边距),它会再次关闭。

以下是(简化的)布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:bind="http://schemas.android.com/apk/res-auto"
  4. xmlns:card_view="http://schemas.android.com/apk/res-auto">
  5. <data>
  6. <!-- ViewModel -->
  7. </data>
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="vertical">
  12. <FrameLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:onClick="@{() -> viewModel.onClickItemContainer()}"
  16. android:clickable="true">
  17. <androidx.cardview.widget.CardView
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content">
  20. <androidx.constraintlayout.widget.ConstraintLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:animateLayoutChanges="true">
  24. <include
  25. android:id="@+id/item_main"
  26. layout="@layout/item_always_visible"
  27. bind:viewModel="@{viewModel}"
  28. bind:layout_constraintTop_toTopOf="parent"
  29. bind:layout_constraintStart_toStartOf="parent"/>
  30. <include
  31. layout="@layout/item_details"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. bind:viewModel="@{viewModel}"
  35. bind:layout_constraintTop_toBottomOf="@id/item_main"
  36. bind:layout_constraintStart_toStartOf="parent" />
  37. </androidx.constraintlayout.widget.ConstraintLayout>
  38. </androidx.cardview.widget.CardView>
  39. </FrameLayout>
  40. </LinearLayout>
  41. </layout>

包含的布局(可以切换开/关):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools">
  5. <data>
  6. <!-- ViewModel -->
  7. <import type="android.view.View" />
  8. </data>
  9. <androidx.constraintlayout.widget.ConstraintLayout
  10. android:id="@+id/entry_details"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:visibility="@{viewModel.detailsVisible ? View.VISIBLE : View.GONE}"
  14. tools:visibility="visible"
  15. android:clickable="false">
  16. <androidx.recyclerview.widget.RecyclerView
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. app:adapter="@{viewModel.adapter}"
  20. app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
  21. app:layout_constraintStart_toStartOf="parent"
  22. app:layout_constraintTop_toTopOf="parent"
  23. android:clickable="false"/>
  24. </androidx.constraintlayout.widget.ConstraintLayout>
  25. </layout>

在RecyclerView中使用的项目也设置为clickable="false"

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout>
  3. <data>
  4. </data>
  5. <TextView
  6. android:id="@+id/detail_text"
  7. xmlns:android="http://schemas.android.com/apk/res/android"
  8. style="@style/Text.SecondaryText"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:clickable="false"/>
  12. </layout>
英文:

I have an onclicklistener on a layout, which contains two includes.
One of the includes contains a constraint layout which will be made visible on click of the container.
It should also be made invisible again, if I click on the layout again.
It works, if I click in the part that was always visible. However, if I click in the area of the newly added constraint layout, nothing happens.
The onclick-listener does not fire.
It works anywhere around the include though. E.g. clicking on the left of it (left there's a margin in original) it closes again.
Here are the (simplified) layouts:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:bind=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:card_view=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
  5. &lt;data&gt;
  6. //Viewmodel
  7. &lt;/data&gt;
  8. &lt;LinearLayout
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;wrap_content&quot;
  11. android:orientation=&quot;vertical&quot;&gt;
  12. &lt;FrameLayout
  13. android:layout_width=&quot;match_parent&quot;
  14. android:layout_height=&quot;wrap_content&quot;
  15. android:onClick=&quot;@{() -&gt; viewModel.onClickItemContainer()}&quot;
  16. android:clickable=&quot;true&quot;&gt;
  17. &lt;androidx.cardview.widget.CardView
  18. android:layout_width=&quot;match_parent&quot;
  19. android:layout_height=&quot;wrap_content&quot;
  20. &gt;
  21. &lt;androidx.constraintlayout.widget.ConstraintLayout
  22. android:layout_width=&quot;match_parent&quot;
  23. android:layout_height=&quot;wrap_content&quot;
  24. android:animateLayoutChanges=&quot;true&quot;&gt;
  25. &lt;include
  26. android:id=&quot;@+id/item_main&quot;
  27. layout=&quot;@layout/item_always_visible&quot;
  28. bind:viewModel=&quot;@{viewModel}&quot;
  29. bind:layout_constraintTop_toTopOf=&quot;parent&quot;
  30. bind:layout_constraintStart_toStartOf=&quot;parent&quot;/&gt;
  31. &lt;include
  32. layout=&quot;@layout/item_details&quot;
  33. android:layout_width=&quot;match_parent&quot;
  34. android:layout_height=&quot;wrap_content&quot;
  35. bind:viewModel=&quot;@{viewModel}&quot;
  36. bind:layout_constraintTop_toBottomOf=&quot;@id/item_main&quot;
  37. bind:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
  38. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  39. &lt;/androidx.cardview.widget.CardView&gt;
  40. &lt;/FrameLayout&gt;
  41. &lt;/LinearLayout&gt;
  42. &lt;/layout&gt;

Included layout (that toggles on/off):

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
  5. &lt;data&gt;
  6. //Viewmodel
  7. &lt;import type=&quot;android.view.View&quot; /&gt;
  8. &lt;/data&gt;
  9. &lt;androidx.constraintlayout.widget.ConstraintLayout
  10. android:id=&quot;@+id/entry_details&quot;
  11. android:layout_width=&quot;match_parent&quot;
  12. android:layout_height=&quot;match_parent&quot;
  13. android:visibility=&quot;@{viewModel.detailsVisible? View.VISIBLE : View.GONE}&quot;
  14. tools:visibility=&quot;visible&quot;
  15. android:clickable=&quot;false&quot;&gt;
  16. &lt;androidx.recyclerview.widget.RecyclerView
  17. android:layout_width=&quot;match_parent&quot;
  18. android:layout_height=&quot;match_parent&quot;
  19. app:adapter=&quot;@{viewModel.adapter}&quot;
  20. app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot;
  21. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  22. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  23. android:clickable=&quot;false&quot;/&gt;
  24. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  25. &lt;/layout&gt;

I've tried around with clickable="false" in a couple of places in the included layout already, because I found that suggestion in another question, but it did not help anything.
If it was affecting the whole extended area (also cardview below include, e.g) I'd be thinking it's a bug with Android not adapting click areas to layout changes, but since I can trigger the listener by clicking on left and below included part, it must be something else.

The items used in the recyclerview are also set to clickable = "false":

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;layout&gt;
  3. &lt;data&gt;
  4. &lt;/data&gt;
  5. &lt;TextView
  6. android:id=&quot;@+id/detail_text&quot;
  7. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  8. style=&quot;@style/Text.SecondaryText&quot;
  9. android:layout_width=&quot;wrap_content&quot;
  10. android:layout_height=&quot;wrap_content&quot;
  11. android:clickable=&quot;false&quot;/&gt;
  12. &lt;/layout&gt;

答案1

得分: 1

显然,这是recyclerviews已知的问题,它们捕获点击事件,因为它们用于滚动。
可以在以下链接找到一些解决方案:
https://stackoverflow.com/questions/30419898/parent-click-event-not-firing-when-recyclerview-clicked
在我的情况下,将recyclerview设置为不可触摸是一个合理的方法,因为所有元素已经位于另一个recyclerview中,所以它们永远不会被截断,而是填充所需的空间。

英文:

Apparently that is a known issue with recyclerviews, that they catch the click event, because they use it for scrolling.
A number of solutions can be found here:
https://stackoverflow.com/questions/30419898/parent-click-event-not-firing-when-recyclerview-clicked
In my case, making the recyclerview untouchable is a reasonable approach, because all elements are already in another recyclerview, so they will never get chopped and instead fill out the space required.

huangapple
  • 本文由 发表于 2023年7月31日 18:34:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定