Close navigation drawer in Android by tapping on the empty portion of the screen

天大地大妈咪最大 提交于 2019-12-11 13:48:02

问题


I have navigation drawer in my Android app and its visibility is controlled by the App icon in the Action Bar. However, I need to be able to close the drawer not only by tapping the app icon but also if the user taps on the empty (non-occupied) portion of the screen. This is my main layout:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"

android:layout_width="match_parent"
android:layout_height="match_parent">

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

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"

    android:background="@color/white"
    style="@style/ListViewSeparator"/>

I don't understand which layout is the one I need to set a listener to, because when I open the navigation drawer it contains my '240dp' listview, but the rest of the screen becomes like 'transparent black', I am looking for that 'transparent black' layout which is the rest of the screen


回答1:


Found it:

 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

it was set to LOCK_MODE_LOCKED_OPENED before




回答2:


Depends on your interests. Based on the Google Examples, i implemented the same using this code:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
...
@Override
public void onItemClick(AdapterView<?> adapterView, View v, int position, long arg3) {
    selectItem(position);

    mDrawerList.setItemChecked(position, true);
    mDrawerLayout.closeDrawer(mDrawerList);
}


来源:https://stackoverflow.com/questions/21828185/close-navigation-drawer-in-android-by-tapping-on-the-empty-portion-of-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!