Android:如何在片段中正确使用ViewPager +操作栏选项卡?

时间:2014-03-25 13:42:54

标签: android android-fragments android-fragmentactivity

这是我的activity_main.xml:

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

<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/frameContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/listMainMenu"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/color_list_mainmenu_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_item_mainmenu_selector_background"
    android:background="@color/color_list_mainmenu_background"/>

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

所以在我的MainActivity类中,我可以显示我的主菜单,我可以显示一个新的片段:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    String title = null;
    switch (position) {
        case 1:
            fragment = new WorkoutStoreFragment();
            title = getString(R.string.mainmenu_item_workout);
            break;
        ....
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frameContainer, fragment).commit();

        // update selected item and title, then close the drawer
        this.listMainMenu.setItemChecked(position, true);
        this.listMainMenu.setSelection(position);
        setTitle(title);
        this.drawerLayout.closeDrawer(this.listMainMenu);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

但是在我的WorkoutStoreFragment类(扩展v4.support.Fragment)中,我想使用带有TabPageIndicator类的viewPager来显示一些不同的其他片段。但它不起作用。

一般来说,我的视图架构是否正确?

感谢前进的人!

0 个答案:

没有答案