动感biubiu 2022-06-17 00:05 采纳率: 46.2%
浏览 140
已结题

Android ScrollView点击穿透问题

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

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

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

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

img

  • 写回答

2条回答 默认 最新

  • 三杯五岳 2022-06-17 11:19
    关注

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

    如非要如此实现,当然也是可以的:
    你这个需求需要让 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月25日
  • 已采纳回答 6月17日
  • 创建了问题 6月17日

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料