子ScrollView截取/阻止/吞噬父视图的点击事件

4
以下是布局示意图:
<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>

我在代码中为父容器RelativeLayout添加了一个OnClickListenter。

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"。请尝试并告诉我结果。 - Liem Vo
我也遇到了同样的问题。@OliverPearmain 如果你有解决方案,能否发布答案? - Rohit
2个回答

0

声明你的ScrollView为不可点击和不可聚焦,以便将点击事件传递给父视图。

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

谢谢您的回答,但不幸的是这个方法似乎行不通。我已经尝试过android:clickable="false",因为我在类似问题的其他答案中看到了这个建议。 - Oliver Pearmain

-1

ScrollView 总是需要一个子布局。 ScrollView 永远不会直接将子视图作为 View。因此,在 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>

感谢您的建议,但不幸的是,这似乎无法解决问题。 - Oliver Pearmain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接