Android触摸事件分发机制之requestDisallowInterceptTouchEvent

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/confusing_awakening/article/details/79501607

这里写图片描述

有时候需要在ScrollView中放置一个能旋转或者其他触摸操作的控件,这时候,ScrollView就不能拦截触摸事件,必须在OnInterceptTouchEvent 返回false。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <FrameLayout
            android:id="@+id/framelayout"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:background="#cccccc">

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:scaleType="centerInside"
                android:src="@drawable/zhuanpan_bg" />

            <ImageView
                android:id="@+id/iv_anniu"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:scaleType="centerInside"
                android:src="@drawable/zhuanpan_anniu" />
        </FrameLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="jifjhe\n\n\n\n\nfjhe\nfjhe\nfjhe\nfjhe\n\n\n\\n\n\\n\n\jhuhuihui\n"
            android:textSize="40sp" />
    </LinearLayout>
</ScrollView>
public class MainActivity extends AppCompatActivity {
    private FrameLayout frameLayout;
    private ScrollView scrollView;
    private ImageView iv_anniu;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        linearLayout = (LinearLayout) findViewById(R.id.layout);
        frameLayout = (FrameLayout) findViewById(R.id.framelayout);

        iv_anniu = (ImageView) findViewById(R.id.iv_anniu);
        iv_anniu.setColorFilter(0xffff0000);
        iv_anniu.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);

//                if(event.getAction() == MotionEvent.ACTION_UP){
//                    //允许ScrollView截断点击事件,ScrollView可滑动
//                    scrollView.requestDisallowInterceptTouchEvent(false);
//                }else{
//                    //不允许ScrollView截断点击事件,点击事件由子View处理
//                    scrollView.requestDisallowInterceptTouchEvent(true);
//                }
                return true;
            }
        });
    }

}

参考:Android自定义控件

猜你喜欢

转载自blog.csdn.net/confusing_awakening/article/details/79501607
今日推荐