Scrollview的requestDisallowInterceptTouchEvent [英] requestDisallowInterceptTouchEvent for Scrollview

查看:96
本文介绍了Scrollview的requestDisallowInterceptTouchEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在scrollview内的linearlayout内有一个StreetViewPanoramaView:

I have a StreetViewPanoramaView inside a linearlayout inside a scrollview:

  <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollview">

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

            <com.google.android.gms.maps.StreetViewPanoramaView
                android:id="@+id/street_view_panorama"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

                <other views - textviews etc>

        </LinearLayout>

    </ScrollView>

StreetViewPanoramaView允许用户上下左右移动街景相机.我希望当用户移动相机时,scrollview停止拦截用户的触摸事件.当前,如果用户在StreetViewPanoramaView中移动摄像机,则滚动视图将拦截触摸并移动滚动视图.

The StreetViewPanoramaView allows a user to move the streetview camera up and down and left and right. I want the scrollview to stop intercepting the user touch events when the user is moving the camera. Currently, if the user moves the camera in the StreetViewPanoramaView, the scrollview intercept the touch and moves the scrollview instead.

我在 https://mobiarch.wordpress.com/2013/11/21/prevent-touch-event-theft-in-android/,但它似乎不起作用(scrollview仍在进行触摸事件):

I tried this code from https://mobiarch.wordpress.com/2013/11/21/prevent-touch-event-theft-in-android/ but its doesn't seem to be working (scrollview is still taking the touch events):

      mStreetViewPanoramaView = (StreetViewPanoramaView) findViewById(R.id.street_view_panorama);
        LinearLayout.LayoutParams streetViewLp =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, displaymetrics.widthPixels/3);
        mStreetViewPanoramaView.setLayoutParams(streetViewLp);

        disableTouchTheft(mStreetViewPanoramaView);

public static void disableTouchTheft(View view) {
    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            view.getParent().getParent().requestDisallowInterceptTouchEvent(true);
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                view.getParent().getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
            return false;
        }
    });
}

我尝试过的事情:

我做了一个自定义视图,并将代码dispatchTouchEvent导入了我的活动代码:

I have made a custom view and imported the code dispatchTouchEvent into my activity code:

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        int[] loc = {0, 0};
        mStreetViewPanoramaView.getLocationOnScreen(loc);

        Rect rect = new Rect(loc[0], loc[1] , loc[0] + mStreetViewPanoramaView.getWidth(), loc[1] + mStreetViewPanoramaView.getHeight());


        if (rect.contains((int) event.getRawX(), (int) event.getRawY())) {
            Log.e("stretview", "dispatched");
            return mStreetViewPanoramaView.dispatchTouchEvent(event);
/*            return true;*/

        }else{
            //Continue with touch event
            return super.dispatchTouchEvent(event);
        }
    }

public class CustomStreetViewPanoramaView extends StreetViewPanoramaView {

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

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

    public CustomStreetViewPanoramaView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomStreetViewPanoramaView(Context context, StreetViewPanoramaOptions options) {
        super(context, options);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return false; // Do not intercept touch event, let the child handle it
    }
}

代码似乎无法正常工作-StreetViewParnoramaView似乎将获得视图上半部分的触摸事件,而下半部分则不会获得任何触摸事件.

The code appears to half-work - it appears that the StreetViewParnoramaView will obtain the touch events for the top half of the view but the bottom half does not obtain any touch events.

我的完整版式文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:id="@+id/container"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollview">

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

            <com.example.simon.customshapes.CustomStreetViewPanoramaView
                android:id="@+id/street_view_panorama"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:id="@+id/card"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hello card"
                    android:layout_gravity="center" />

            </android.support.v7.widget.CardView>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Save panorama"
                android:padding="8dp"
                android:id="@+id/savepano"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v7.widget.RecyclerView>

        </LinearLayout>
    </ScrollView>

</LinearLayout> 

推荐答案

约翰的答案是正确的道路,但仍然行不通-这将行之有效

The answer of Johann is the right path but still not working - this one will work

    @Override
public boolean dispatchTouchEvent(MotionEvent event) {

    if (streetView != null) {
        streetView.getGlobalVisibleRect(rect);

        if (rect.contains((int) event.getRawX(), (int) event.getRawY())) {
            event.offsetLocation(-rect.left, -rect.top);
            streetView.dispatchTouchEvent(event);
            return true;

        } else {
            //Continue with touch event
            return super.dispatchTouchEvent(event);
        }
    } else {
        return super.dispatchTouchEvent(event);
    }
}

活动的dispatchTouchEvent,并将事件偏移到streetView后将其移动

dispatchTouchEvent of the activity and move the event after offsets it to the streetView

这篇关于Scrollview的requestDisallowInterceptTouchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆