Material design navigation drawer not sliding out

2019-07-29 14:25发布

I followed this StackOverflow post's instructions and managed to get the ActionBarDrawerToggle and its animation working. However, no navigation drawer slides out. Why is that?

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame"
android:fitsSystemWindows="true" >

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

<it.neokree.materialtabs.MaterialTabHost
    android:id="@+id/materialTabHost"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:textColor="#FFFFFF"
    app:primaryColor="#33B5E5"
    app:accentColor="#FFFFFF" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Main layout -->
    <FrameLayout
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Nav drawer -->
    <fragment
        android:id="@+id/nav_drawer"
        android:name="com.wsandhu.conjugation.DrawerFragment"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left|start" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>

Here's the code in the onCreate() method in MainActivity.java:

ActionBarDrawerToggle mDrawerToggle;
DrawerLayout mDrawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
    mDrawerLayout.setDrawerListener(mDrawerToggle);

...

    if (savedInstanceState == null) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new MainFragment())
                        .commit();
            }
        });
    }

...

}

I have a DrawerFragment class and its XML file, but as I said, nothing slides out when I tap the ActionBarDrawerToggle in my app. The toggle animates perfectly.

The five methods that needed to be overrided as stated in the other post are in my MainActivity, after the onCreate() method. I really don't know what else to add, I can't figure this out.

Thanks in advance.

1条回答
干净又极端
2楼-- · 2019-07-29 15:25

I did not understand DrawerLayout, in part due to jpardogo's answer.

I needed to set the proper main content frame in my activity's XML file, so I made android.support.v4.widget.DrawerLayout the root element of the file, and put my activity's LinearLayout above the navigation drawer fragment element. Here is the proper, working code:

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

<!-- Main layout -->
<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame"
    android:fitsSystemWindows="true" >

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

    <it.neokree.materialtabs.MaterialTabHost
        android:id="@+id/materialTabHost"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        app:textColor="#FFFFFF"
        app:primaryColor="#33B5E5"
        app:accentColor="#FFFFFF" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<!-- Nav drawer -->
<fragment
    android:id="@+id/nav_drawer"
    android:name="com.wsandhu.conjugation.DrawerFragment"
    android:layout_width="@dimen/drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="left|start" />

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

It would help to compare this to the XML file in the original post. Hope this helps.

查看更多
登录 后发表回答