10

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>
Dinesh Anuruddha
  • 7,137
  • 6
  • 32
  • 45

6 Answers6

4

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>
Piyush
  • 18,895
  • 5
  • 32
  • 63
MIkka Marmik
  • 1,101
  • 11
  • 29
2

It's the drawer's behavior with action bar tabs. You can't change it.

Find some official information in this answer: Android Navigation Drawer over the tabs

And workarounds in this answer: Getting DrawerLayout to Slide over the ActionBar

I would consider using this great library: PagerSlidingTabStrip

Community
  • 1
  • 1
Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
  • The google games app, if you select my games in the drawer it will launch a view pager with tabs and the drawer appears above all. – Maxrunner Jul 30 '13 at 15:49
  • Those are not action bar tabs. – Matthias Robbers Jul 30 '13 at 17:54
  • The tabs in the Google Play Games app. – Matthias Robbers Jul 31 '13 at 14:40
  • For the record, Google doesn't want you to overlap the action bar with the drawer. I've gotten this response from within Google and it's also stated in the Navigation Drawer documentation. "As the navigation drawer expands, it overlays the content but not the action bar" source: http://developer.android.com/design/patterns/navigation-drawer.html – Martin Marconcini Nov 01 '13 at 17:03
1

I am not sure about Action Bar tabs , but you can use Pager Tab Strip in combination of Navigation Drawer to get navigation model similar like Google Play Music , have a look at my post

Community
  • 1
  • 1
Balaji
  • 1,619
  • 15
  • 19
0

Your XML is incorrect.

ListView should set layout_height to match_parent instead of wrap_content.

EDIT:

also the DrawerLayout should be toplevel instead of being squeezed into LinearLayout. Check the documentation of how to use DrawerLayout: http://developer.android.com/training/implementing-navigation/nav-drawer.html

Damian Walczak
  • 1,334
  • 14
  • 21
0

Have you used FrameLayout? The Content shown on the last FrameLayout will show on the top. You hide that "layer" and the FrameLayout "below" it (FrameLayout before last FrameLayout), will show up. In Your DrawerLayout, Drawer List should be in the last FrameLayout and Fragments in the second last, in which you can show the ViewPager.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                 >

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

                    <!-- Fragments will attach in this container, and have ViewPager -->
                </LinearLayout>


            </LinearLayout>
        </FrameLayout>


        <!-- attach drawer -->

        <FrameLayout
            android:id="@+id/drawer"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/grey"
            android:choiceMode="singleChoice"
            android:clickable="true"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00000000"
                android:orientation="vertical" >

                    <ListView
                    android:id="@+id/navigation_drawer_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:choiceMode="singleChoice"
                    android:groupIndicator="@null" />

                <!-- </LinearLayout> -->
            </LinearLayout>
        </FrameLayout>
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>
Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
0

To display drawer layout just below the toolbar use code-below in your activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

            <android.support.design.widget.NavigationView
                android:id="@+id/navigation_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:fitsSystemWindows="true"
                app:menu="@menu/activity_main_drawer" />

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

</LinearLayout>  
Farid
  • 2,317
  • 1
  • 20
  • 34