settaratici
settaratici

Reputation: 309

Android Tabs inside sliding drawer

I want to create a navigation drawer that contains tabs in android, but I could not do anything. I want to add tabs (categories, favorites) to the navigation drawer.Is it posible?

(eg. ios)

enter image description here

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, 
            R.string.app_name, R.string.app_name ){
        public void onDrawerClosed(View view) {
            // calling onPrepareOptionsMenu() to show action bar icons
            // invalidateOptionsMenu();

        }

        public void onDrawerOpened(View drawerView) {
            // calling onPrepareOptionsMenu() to hide action bar icons
            //invalidateOptionsMenu();
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);

    tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    tabHost.setup(getApplicationContext(), getSupportFragmentManager(), android.R.id.tabcontent );

    tabHost.addTab(tabHost.newTabSpec("surec").setIndicator("Süreç listesi"), SurecListesiFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec("bolum").setIndicator("Bölüm listesi"), PhotosFragment.class, null);

I cannot switch between tabs. And I cannot use any widgets on tab content. (listview, button, etc.. )

(EDIT : solution) (linearlayout : slider menu layout)

linearLayout.bringToFront();
linearLayout.requestLayout();

Upvotes: 2

Views: 741

Answers (1)

Juanjo Vega
Juanjo Vega

Reputation: 1430

I did it just yesterday.

This is my layout for main activity with drawer:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Main layout -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/app_background"
        android:orientation="vertical" >

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/layout_location_spinners_campaigns" />

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/layout_campaignslistview" />
    </LinearLayout>

    <!-- Slider menu -->

    <LinearLayout
        android:id="@+id/preferencesDrawer"
        android:layout_width="290dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="@color/app_background"
        android:orientation="vertical" >

        <android.support.v4.app.FragmentTabHost
            android:id="@android:id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

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

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:orientation="horizontal" />

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="0" />

                <FrameLayout
                    android:id="@+id/realtabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />
            </LinearLayout>
        </android.support.v4.app.FragmentTabHost>
    </LinearLayout>

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

The method to display the tabs:

FragmentTabHost tabhost;

void buildTabs() {
    tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    tabhost.addTab(tabhost.newTabSpec("locations").setIndicator(getString(R.string.locations)), FragmentPreferencesLocations.class, null);
    tabhost.addTab(tabhost.newTabSpec("categories").setIndicator(getString(R.string.categories)),FragmentPreferencesCategories.class, null);
}

Besides that you need code to display the drawer and customize your tabs...


EDIT:

I initialize drawer with this method (invoke it at onCreate):

ActionBarDrawerToggle drawerToggle;
LinearLayout preferencesDrawer;
FragmentTabHost tabhost;
...

void initDrawer() {
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.app_name,
            R.string.app_name) {
        public void onDrawerClosed(View drawerView) {
            getSupportActionBar().setTitle(getString(R.string.app_name));
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
            drawerClosed(drawerView);
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(getString(R.string.preferences));
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}

// Invoked by action bar button
void clickFilterCategories() {
    toggleDrawer(preferencesDrawer);
}

void toggleDrawer(View drawer) {
    if (drawerLayout.isDrawerOpen(drawer)) {
        closeDrawer(drawer);
    } else {
        openDrawer(drawer);
    }
}

void openDrawer(View toOpen) {
    drawerLayout.openDrawer(toOpen);
}

void closeDrawer(View toClose) {
    drawerLayout.closeDrawer(toClose);
}

Upvotes: 1

Related Questions