【问题标题】:Child ScrollView Intercepting/Blocking/Swallowing Clicks to its Parent View子 ScrollView 拦截/阻止/吞咽对其父视图的点击
【发布时间】:2014-04-04 10:20:55
【问题描述】:

给定以下布局:

<RelativeLayout
    android:id="@+id/item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageview_item"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:scaleType="fitXY"
        android:adjustViewBounds="false" />

    <ScrollView
        android:id="@+id/scrollview_description"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageview_item"
        android:scrollbars="vertical"
        android:fillViewport="true">

        <TextView
            android:id="@+id/textView_description"
            style="@style/TextDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ScrollView>

</RelativeLayout>

在代码中,我已将 OnClickListenter 添加到父/容器 relativeLayout。

View itemView = (View) rootView.findViewById(R.id.item);

itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Do stuff
    });

如果点击 ImageView,这可以正常工作,但是如果点击描述 ScrollView/TextView 区域,则永远不会触发 OnClickListenter 的 onClick() 方法,可能是因为 ScrollView 正在处理/吞咽触摸。

如何从 ScrollView 传递标准点击事件,以确保触发其父 relativeLayout OnClickListenter.onClick() 方法? (但显然 ScrollView 还是可以滚动的)

谢谢

【问题讨论】:

  • 在布局的滚动视图标签中,请添加android:clickable="false"。请试一试,让我知道结果
  • 我面临同样的问题。 @OliverPearmain 如果您有解决方案,您可以发布答案吗??

标签: android scrollview


【解决方案1】:

声明您的 ScrollView non clickable and non focusable 以将点击事件传递给 parentView。

android:clickable="false"
android:focusable="false" 

【讨论】:

  • 感谢您的回答,但不幸的是,这似乎不起作用。我已经尝试过 android:clickable="false" 正如我在其他类似问题的答案中看到的那样。
【解决方案2】:

ScrollView 总是需要一个子元素作为布局。 ScrollView 永远不会有直接子视图。因此,在滚动视图内创建一个布局,并在该布局内添加您的TextView,如下所示:

这里我添加了LinearLayout你可以根据你的选择添加布局。

 <ScrollView
    android:id="@+id/scrollview_description"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageview_item"
    android:scrollbars="vertical"
    android:fillViewport="true">

    <LinearLayout
          android:id="@+id/linerlayout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="vertical">
    <TextView
        android:id="@+id/textView_description"
        style="@style/TextDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
   </LinearLayout>
</ScrollView>

【讨论】:

  • 感谢您的建议,但不幸的是,这似乎无法解决问题。
猜你喜欢
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 2017-01-26
  • 2020-03-29
相关资源
最近更新 更多