【问题标题】:Why parent linearLayout consumes all clicks of children?为什么父级linearLayout会消耗所有子级的点击?
【发布时间】:2018-03-13 18:40:51
【问题描述】:

在我的活动中,我使用 bottom sheet 布局 - 即带有 ID bottom_sheet 的 NestedScrollView。

在 NestedScrollView 中,我有 LinearLayout,其中包含另一个布局 (layout_player)。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/cl_root"
    >

       <some layouts here />

    </android.support.constraint.ConstraintLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        android:visibility="invisible"
    >

    <LinearLayout
        android:id="@+id/player_container"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="bottom"
    >

      <include layout="@layout/layout_player"
      />

    </LinearLayout>
  </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

包含的layout_playerlayout 由带有一些 Guidelines 的 ConstraintLayout 和带有 ImageView 的 RelativeLayout 组成。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
>
 ...
<RelativeLayout
      android:id="@+id/rl_play"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      app:layout_constraintTop_toTopOf="@id/guidelineTop"
      app:layout_constraintBottom_toBottomOf="@id/guidelineBottom"
      app:layout_constraintDimensionRatio="H,1:1"
  >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ic_play"
    />

  </RelativeLayout>
...
</android.support.constraint.ConstraintLayout>

在我为布局 layout_player 中的视图设置 clickListeners 之后 - 具有 ID player_container 的父 LinearLayout 消耗其子级的所有点击。但我需要孩子的点击事件 - 而不是父母。

如何在其父级中启用点击子级?

在我的情况下,如何获取位于 LinearLayout 内的 ContraintLayout 内的 RelativeLayout 的点击事件?

【问题讨论】:

    标签: android android-layout android-view bottom-sheet


    【解决方案1】:

    你要找的是descendantFocusability

    在你不想要焦点的布局上,你可以这样做:

    android:descendantFocusability="afterDescendants"
    

    这意味着它将在布局获得焦点之前关注子视图。

    编辑(举例):

    <LinearLayout
        android:id="@+id/player_container"
        android:focusable="false"
        android:descendantFocusability="afterDescendants"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="bottom">
    
        <include layout="@layout/layout_player" />
    
    </LinearLayout>
    

    然后在layout_player 文件中:

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:descendantFocusability="afterDescendants"
        android:layout_width="match_parent"
        android:layout_height="100dp" >
    
     ...
        <RelativeLayout
            android:id="@+id/rl_play"
            android:clickable="true"
            android:focusable="true"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="@id/guidelineTop"
            app:layout_constraintBottom_toBottomOf="@id/guidelineBottom"
            app:layout_constraintDimensionRatio="H,1:1">
    
            ...
    
        </RelativeLayout>
    ...
    </android.support.constraint.ConstraintLayout>
    

    所以我们在两个不同的布局上添加android:descendantFocusability="afterDescendants 行,并允许点击并通过这样做来关注RelativeLayout

    android:clickable="true"
    android:focusable="true"
    

    注意:如果这不起作用,您能否说明如何在 RelativeLayout 上设置侦听器?

    【讨论】:

    • 感谢您的回答。它对我没有帮助。它看起来像 ScrollView 内的 ConstraintLayout 无法正常工作。
    • 试试上面的例子,如果不行,请展示你的代码,告诉你如何设置RelativeLayout和其他的监听器
    • 嗨@THEPATEL。感谢你的回复。还是不行。这是我的课程,我在其中找到视图并设置点击侦听器codeshare.io/aJWWVX
    • @THEPATEL 在这里你也可以看到我的 Activity 布局codeshare.io/albbrj 和 layout_player_mini.xml codeshare.io/GAmmR8
    猜你喜欢
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多