ViewPager +片段+导航抽屉和切换片段

时间:2013-12-26 18:43:44

标签: android android-fragments android-viewpager android-fragmentactivity navigation-drawer

我的应用包含托管NavigationDrawer的MainActivity。左侧抽屉包含带有菜单项的ListView。单击列表菜单中的项目会在content_frame上切换Fragment。抽屉的初始内容是ViewPager,它通过FragmentPagerAdapter创建Fragments。其余菜单项强制通过事务创建单个片段。 问题是所有片段都会将项目填充到菜单中。有时,菜单项会多次显示(仅来自1个片段类型)。 如何确保正确替换的Fragment从ActionBar中删除其菜单项,并且新创建的Fragment正确地将项添加到ActionBar中?

enter image description here

请参阅下面的布局:

<?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:paddingBottom="@dimen/activity_vertical_margin" -->
<!-- android:paddingLeft="@dimen/activity_horizontal_margin" -->
<!-- android:paddingRight="@dimen/activity_horizontal_margin" -->
<!-- android:paddingTop="@dimen/activity_vertical_margin" -->

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</FrameLayout>

<FrameLayout
    android:id="@+id/left_drawer"
    android:layout_width="340dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111" >

    <fragment
        android:id="@+id/fragmentDrawerMenu"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="pl.cubesoft.photoviewer.fragment.DrawerMenuFragment" />

</FrameLayout>

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

切换片段的代码:

    private boolean selectMenuOption(int position) {
    boolean result = false;

    switch (position) {
    case 0: {
        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content_frame, MainPagerFragment.newInstance());
        ft.commit();



        result = true;
        break;
    }
    case 1:{

        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content_frame, UploadMediaListFragment.newInstance());
        ft.commit();

        /*mUploadMediaListFragment.initialize(getPhotoViewerApplication().getUploadManager(), 
                getPhotoViewerApplication().getImageLoader());
        */


        result = true;
        break;
        }

    case 2: {

        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content_frame, LocalMediaGridFragment.newInstance());
        ft.commit();


        result = true;
        break;

    }

    case 3: {

        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content_frame, AppPreferencesFragment.newInstance(R.xml.main_preferences));
        ft.commit();


        result = true;
        break;

    }

    }

    if (result && mDrawerLayout.isDrawerOpen(mLeftDrawer)) {
        mDrawerLayout.closeDrawer(mLeftDrawer);
    } 
    return false;
}

MainPagerFragment包含ViewPager。它的子代是从FragmentStatePagerAdapter填充的片段:

@Override
public Fragment getItem(int position) {
    Fragment result = null;
    if ( mProvider == null ) {
        return null;
    }

    switch (mProvider.getViewType(mUserId, position)) {
    case PHOTO_GROUP_STREAM: {
        final PhotoGroupsGridFragment fragment = PhotoGroupsGridFragment.newInstance(mUserId);
        //final PhotoGroupsProvider photoGroupProvider = mProvider.getPhotoGroupsProvider(mUserId);
        //fragment.initialize(photoGroupProvider, mImageLoader, mUserId);

        result = fragment;
        break;
    }
    case PHOTO_STREAM: {
        final PhotoGroupGridFragment fragment = PhotoGroupGridFragment.newInstance(mUserId, GroupType.FAVOURITE_GROUP, 0);
        //fragment.initialize(mProvider.getPhotoGroupProvider(mUserId, GroupType.FAVOURITE_GROUP, 0), mImageLoader);

        result = fragment;
        break;
    }

    case PHOTO_RECENT_STREAM: {
        final PhotoGroupGridFragment fragment = PhotoGroupGridFragment.newInstance(mUserId, GroupType.RECENT_GROUP, 0);
        //fragment.initialize(mProvider.getPhotoGroupProvider(mUserId, GroupType.RECENT_GROUP, 0), mImageLoader);
        /*fragment.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                    int position, long id) {


                //ActivityHelper.showPhotoGroupActivity( mContext, position );
            }
        });*/
        result = fragment;
        break;
    }

    case PHOTO_USER_STREAM: {
        final PhotoGroupGridFragment fragment = PhotoGroupGridFragment.newInstance(mUserId, GroupType.STREAM_GROUP, 0);
        //fragment.initialize(mProvider.getPhotoGroupProvider(mUserId, GroupType.STREAM_GROUP, 0), mImageLoader);
        /*fragment.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                    int position, long id) {


                //ActivityHelper.showPhotoGroupActivity( mContext, position );
            }
        });*/
        result = fragment;
        break;
    }

    case USER_LIST: {
        final UserListFragment fragment = UserListFragment.newInstance(UserListType.FRIENDS_LIST);

        result = fragment;
        break;
    }

    case CONTACT_LIST: {
        final UserListFragment fragment = UserListFragment.newInstance(UserListType.CONTACTS_LIST);

        result = fragment;
        break;
    }

    case PHOTO_FEATURED_STREAM: {
        final PhotoGroupGridFragment fragment = PhotoGroupGridFragment.newInstance(mUserId, GroupType.FEATURED_GROUP, 0);
        result = fragment;
        break;
    }

    case COMMENT_STREAM: {
        final PhotoCommentsFragment fragment = PhotoCommentsFragment.newInstance(mUserId, null, null);

        result = fragment;
        break;
    }

    }

    return result;
}

0 个答案:

没有答案