Android DrawerLayout - 让孩子们关注焦点

时间:2015-01-21 18:36:35

标签: android android-layout android-fragments

抽屉是否有可能阻止儿童接收触摸事件:

这是我的xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- THE MENU -->
<RelativeLayout
    android:id="staic_menu"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/mainDrawer">
    <Button
        android:layout_width="300dp"
        android:layout_width="wrap_content"
        android:text="Click Me"
    />
</RelativeLayout>

<android.support.v4.widget.DrawerLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </RelativeLayout>

    <!-- LEFT DRAWER -->
    <RelativeLayout
        android:id="@+id/drawerView"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:visibility="gone">

    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

当抽屉打开时,按钮会被忽略..但是当它关​​闭时,按钮会起作用。

谢谢,

1 个答案:

答案 0 :(得分:2)

您需要使用自定义抽屉。

与默认区别不同:

  1. 不会自动关闭
  2. 打开时不会使屏幕变暗
  3. 点击抽屉外部时不会拦截触摸
  4. 以下代码有这些不利条件/限制:

    • 将使用左侧的抽屉(由于宽度检查)
    • 将假设DrawerLayout的第二个孩子是ListView(或其他一些抽屉内容)。
    • 不能关闭默认方式(滑动),只能使用后退按钮。您可以自己实现(需要在某些时候手动启用/禁用锁定模式)。

    <强> UnobtrusiveDrawer.java:

    public class UnobtrusiveDrawer extends DrawerLayout {
    
        public UnobtrusiveDrawer(Context context) {
            super(context);
        }
    
        public UnobtrusiveDrawer(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public UnobtrusiveDrawer(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            View drawer = getChildAt(1);
            Log.e("TAG", "Ev: " + ev.getRawX() + " " + drawer.getWidth());
            if (isDrawerOpen(drawer) && ev.getRawX() > drawer.getWidth()) {
                Log.e("TAG", "Drawer open, and click outside");
                return false;
            } else {
                Log.e("TAG", "Drawer closed or click on it");
                return true;
            }
        }
    
    }
    

    <强> MainActivity.java:

    import android.os.Bundle;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    
    public class MainActivity extends ActionBarActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
            drawer.setScrimColor(getResources().getColor(android.R.color.transparent));
            drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
                @Override
                public void onDrawerOpened(View drawerView) {
                    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
                }
    
                @Override
                public void onDrawerSlide(View drawerView, float slideOffset) {}
    
                @Override
                public void onDrawerClosed(View drawerView) {}
    
                @Override
                public void onDrawerStateChanged(int newState) {}
            });
        }
    
    }
    

    <强> activity_main.xml中:

    <com.example.myapplication.UnobtrusiveDrawer
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:layout_gravity="right"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
        </FrameLayout>
    
        <ListView
            android:layout_gravity="left"
            android:background="#FFF"
            android:layout_width="250dp"
            android:layout_height="match_parent"/>
    </com.example.myapplication.UnobtrusiveDrawer>