当用户在导航之外触摸时,我正在尝试关闭导航布局。我在布局中添加了一个 touchListener,并在触摸监听器中调用了drawerLayout.closeDrawer(drawer)。但是,这并没有完全关闭抽屉。如果我继续点击抽屉外的屏幕,它会继续关闭。所以触摸事件没有足够长的时间来完全关闭抽屉。点击外部时如何完成关闭抽屉?
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" >