Android 滑动抽屉内的选项卡

Android Tabs inside sliding drawer

我想在 android 中创建一个包含选项卡的导航抽屉,但我无能为力。我想在导航中添加选项卡(类别、收藏夹)drawer.Is 可行吗?

(例如 ios)

 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);

我无法在选项卡之间切换。而且我不能在选项卡内容上使用任何小部件。 (列表视图、按钮等。)

(编辑:解决方案) (线性布局:滑块菜单布局)

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

我昨天刚做的。

这是我的主要 activity 抽屉布局:

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

标签显示方法:

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);
}

此外,您还需要代码来显示抽屉和自定义选项卡...


编辑:

我用这个方法初始化抽屉(在 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);
}