Android抽屉关闭使用触摸侦听器关闭并不是一直关闭

时间:2015-07-20 20:33:59

标签: android android-drawer

我试图在用户触摸导航外时关闭导航布局。我在布局中添加了一个touchListener,并在触摸侦听器中调用了drawerLayout.closeDrawer(抽屉)。然而,这并没有完全关闭抽屉。 如果我一直在抽屉外面点击屏幕,它会继续关闭。所以触摸事件不足以完全关闭抽屉。如何在外面点击时关闭抽屉?

drawerLayout.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub              
            if(drawerLayout.isDrawerOpen(drawer)){
                drawerLayout.closeDrawer(drawer);
            }
            return false;
        }           
    }); 

XML片段:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
.
.
.
<LinearLayout
    android:id="@+id/drawer"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#80000000"
    android:choiceMode="singleChoice"
    android:orientation="vertical" >

0 个答案:

没有答案