Android DrawerLayout is not working with ViewPager?

Dinesh Anuruddha picture Dinesh Anuruddha · Jun 6, 2013 · Viewed 11.7k times · Source

I have implemented ViewPager inside a DrawerLayout it is working properly but Drawer menu list view not displaying properly with the action bar it will display below the ViewPager tabs.Hope following figure will give you an idea. And i am using actionbarsherlock library.

enter image description here

How can i display the Drawer menu list view exactly below the action bar??

What i have tried.

Activity OnCreate():

        setContentView(R.layout.activity_layout);
        mDrawerLayout   = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList     = (ListView) findViewById(R.id.left_drawer);
        mLinearLayout   = (LinearLayout)findViewById(R.id.ll_viewpager_layout);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.app_name,  /* "open drawer" description for accessibility */
                R.string.app_you  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
            }

            public void onDrawerOpened(View drawerView) {
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        invalidateMenuItems();

        mViewPager = new ViewPager(this);
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setId(1);
        mViewPager.setOffscreenPageLimit(7);

        mLinearLayout.addView(mViewPager);

        mTabsAdapter = new TabsAdapter(this, mViewPager,mActionBar);

        mTabsAdapter.addTab(mActionBar.newTab().setText("Fragment"),Fragment.class, null);

        mTabsAdapter.addTab(mActionBar.newTab().setText("Fragment"),Fragment.class, null);

        mTabsAdapter.addTab(mActionBar.newTab().setText("Fragment"),Fragment.class, null);

        mTabsAdapter.addTab(mActionBar.newTab().setText("Fragment"),Fragment.class, null);

Activity Layout xml:

 <?xml version="1.0" encoding="utf-8"?>
<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"
        android:orientation="vertical" >

                <LinearLayout
            android:id="@+id/ll_dashboard_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="200dip"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/abs__background_holo_light"
            android:cacheColorHint="#00000000"
            android:choiceMode="singleChoice"
            android:dividerHeight="1dip" />


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

Answer

MIkka Marmik picture MIkka Marmik · Jul 17, 2014

Drawer layout is parent of view pager. try this

<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" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

    <!-- Framelayout to display Fragments -->

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

    <!-- Listview to display slider menu -->

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

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