Android ScrollView点击穿透问题

如下图:
TextView在下层,ScrollView在上层。

ScrollView头部有一段空白层的透明View,当点击空白层,如何穿透点击到下面的TextView层,而不透明的内容区不允许穿透点击?

找到的答案好像是要重写ScrollView的onTouch方法,但是如何写?

PS:请直接粘代码,不谈理论,谢谢~

img

我的建议也是如果非必须,可以换一种方式实现需求,如楼上说的模拟、或者协调布局等。

如非要如此实现,当然也是可以的:
你这个需求需要让 ScrollView 点击穿透,但有不能全部穿透,仅透明区域。
那么重点在于,ScrollView 本身需要触摸事件,已做滚动,所以需要做好透明区域、不透明区域、ScrollView滚动、穿透点击等的事件分发。

简单做法就是ScrollView 透明区域可穿透点击,其他区域正常滚动,如下:

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView {

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        float y = ev.getY() + getScrollY();
        if (y < DisplayUtils.dip2px(getContext(), 500)) {//500是你透明区域的高度
            return false;//穿透点击
        }
        return super.onTouchEvent(ev);//ScrollView 的正常滚动
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }
}

布局:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:onClick="onLayout1"
            android:orientation="vertical">

        </LinearLayout>

        <com.ba.myapplication.MyScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    android:orientation="vertical"></LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="1000dp"
                    android:background="#ff0000"></LinearLayout>
            </LinearLayout>
        </com.ba.myapplication.MyScrollView>
    </FrameLayout>

</LinearLayout>

如果是在一个界面中。可以监听上层的点击,然后让下层View执行模拟点击来触发下层的点击。 view.performClick()