Search code examples
androidtouchsurfaceviewdrawerlayoutdrawer

DrawerLayout + SurfaceView + TouchListener


I want to use a DrawerLayout together with a SurfaceView (MetaioSDK Augmented Reality View). Everything is working fine except the TouchListener for the SurfaceView. It looks like that the FrameLayout inside the DrawerlLayout blocks my Touch-Events for the SurfaceView. Is it possible to delegate the Event directly to the SurfaceView? I would appreciate some help from some pros.

Below is my xml-layout

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />
</FrameLayout>

<LinearLayout
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tV_Attributes"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:text="Hier sollen Attribute angezeigt werden." />
</LinearLayout>


Solution

  • You need to extend the FrameLayout to ignore touch events, if it was landed on SurfaceView.

    In your FrameLayout:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (isHitToView(ev, view)) {
            return false;
        } else {
            return super.onInterceptTouchEvent(ev);
        }
    }
    
    public static boolean isHitToView(MotionEvent event, View view) {
        if (view == null || event == null || view.getVisibility() != View.VISIBLE)
            return false;
    
        Rect r = new Rect();
        //r.left и r.top - всегда нули
        view.getLocalVisibleRect(r);
    
        int[] coordinates = new int[2];
        view.getLocationOnScreen(coordinates);
    
        r.left += coordinates[0];
        r.right += coordinates[0];
        r.top += coordinates[1];
        r.bottom += coordinates[1];
    
        float x = event.getRawX();
        float y = event.getRawY();
    
        if (r.right >= x && r.top <= y && r.left <= x && r.bottom >= y)
            return true;
        return false;
    }