移动导航抽屉模式的最佳方法是什么?

时间:2013-10-04 07:41:44

标签: android android-activity android-fragments navigation-drawer

我在我的应用中提供了NavigationDrawer。

我在http://developer.android.com/training/implementing-navigation/nav-drawer.html

上使用手册
/**
 * Swaps fragments in the main content view
 */
private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on
    // position
    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);
    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

在这里,我必须将我的屏幕设置为Fragment,但现在我的屏幕是Activity,我想以最少的代码更改从我的Activity移动到Fragment。我能做什么?

2 个答案:

答案 0 :(得分:2)

最好的解决方案只是改变从Activity扩展到Fragment,将onCreate()更改为onActivityCreated()并在onCreateView()上移动layout.xml链接

答案 1 :(得分:0)

参见here 您应该能够将活动转换为片段活动,然后使用导航抽屉。另请参阅here了解示例应用

相关问题