Search code examples
androidactionbarsherlockandroid-viewpagerdrawerlayout

Android DrawerLayout is not working with ViewPager?


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>

Solution

  • 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