AndroidBegginer AndroidBegginer - 9 months ago 25
Android Question

Fragment as Navigation Drawer / Sliding Menu

I've been looking for that for several days, without any luck. I'm trying to make one of my fragments act like a navigationdrawer - to slide from side after a menuitem click. Can anyone give me some clue?

Here's my fragment:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pl.webtube.bejmy.fragments.NotificationFragment">

<android.support.v7.widget.RecyclerView
android:id="@+id/notification_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</FrameLayout>


I've tried inserting it in DrawerLayout, without a luck.

Answer Source

Here's an example of an Activity layout containing a Fragment set to the left side of the screen as a Drawer.

<?xml version="1.0" encoding="utf-8"?>
<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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

    <!-- Things "under the drawer" -->
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <!-- Toolbar -->
        <android.support.design.widget.AppBarLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:theme="@style/AppTheme.AppBarOverlay">

            <include layout="@layout/toolbar"/>

        </android.support.design.widget.AppBarLayout>

        <!-- "Main Activity content", like a FrameLayout to load Fragments -->
        <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/container"/>
    </LinearLayout>

    <!-- The left drawer; change width to adjust size -->
    <LinearLayout
            android:layout_width="64dp" 
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/background_light"
            android:fitsSystemWindows="true" >
        <fragment
                android:id="@+id/fragment_drawer"
                android:name="com.example.NavigationDrawerFragment"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>

    </LinearLayout>


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

And you can load literally any other view inside of com.example.NavigationDrawerFragment. It doesn't need to be a list.