【问题标题】:DrawerLayout + SurfaceView + TouchListenerDrawerLayout + SurfaceView + TouchListener
【发布时间】:2023-03-12 12:05:01
【问题描述】:

我想将 DrawerLayout 与 SurfaceView(MetaioSDK 增强现实视图)一起使用。除了 SurfaceView 的 TouchListener 外,一切正常。 看起来 DrawerlLayout 内的 FrameLayout 阻止了 SurfaceView 的触摸事件。是否可以将事件直接委托给 SurfaceView?我会感谢一些专业人士的帮助。

下面是我的xml布局

<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>

【问题讨论】:

    标签: android touch surfaceview drawerlayout drawer


    【解决方案1】:

    您需要扩展 FrameLayout 以忽略触摸事件,如果它落在 SurfaceView 上。

    在您的 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;
    }
    

    【讨论】:

    • 非常感谢。我的解决方案是以下受您的回答启发的。公共类 CustomDrawerLayout 扩展 DrawerLayout { ... @Override public boolean onTouchEvent(MotionEvent ev) { super.onTouchEvent(ev); if(ev.getX() > 30 && ev.getAction() == MotionEvent.ACTION_DOWN){ Log.d("CustomDrawerLayout", "onTouchEvent: false");返回假; }else{ Log.d("CustomDrawerLayout", "onTouchEvent: true");返回真; } } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    相关资源
    最近更新 更多