简体   繁体   English

导航抽屉和FragmentActivity

[英]Navigation Drawer and FragmentActivity

I'm relatively new to android development. 我是android开发的新手。 Basically, I got a Navigation Drawer. 基本上,我有一个导航抽屉。 One of the menu will need to open FragmentActivity with viewpager. 菜单之一将需要使用viewpager打开FragmentActivity。

here the displayview function 这里的displayview功能

 /**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    LAST_DISPLAY_POSITION = position;

    Fragment fragment = null;
    switch (position) {
    case 0:
        Intent in = new Intent(getApplicationContext(), TestFragmentActivity.class);
        startActivity(in);
        break;
    case 1:
        fragment = new MyAccountFragment();
        break;
    case 2:
        fragment = new TestFragment();
        break;
    case 3:
        fragment = new TestFragment();
        break;
    case 4:
        fragment = new TestFragment();
        break;

    default:
        break;
    }

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

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

as you can see at Case 0, I just calling startactivity to open the view. 如案例0所示,我只是调用startactivity打开视图。 How can I display the FragmentActivity with the Navigation Drawer? 如何使用导航抽屉显示FragmentActivity?

Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT 编辑

my activity_main.xml 我的activity_main.xml

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

    <!-- Framelayout to display Fragments -->

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

    <!-- Listview to display slider menu -->

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

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

Do one thing, right now you might have written code for NavigationDrawer in your single activity say DisplayFragmentsActivity and you are going to another activity TestFragmentActivity which will surely make you write code for NavigationDrawer in that activity too plus you will need to manage all other things for selection of menu if you are handling. 做一两件事,现在你可能已经编写了代码NavigationDrawer在单一的活动说DisplayFragmentsActivity和你要去另一个活动TestFragmentActivity这必将让你写代码NavigationDrawer该活动也加上,你需要管理所有其他的事情选择菜单(如果要处理)。

So, I suggest move your code of NavigationDrawer from DisplayFragmentsActivity to a new MasterFragmentActivity and extend that activity to your DisplayFragmentsActivity and TestFragmentActivity . 因此,我建议将NavigationDrawer的代码从DisplayFragmentsActivity移到新的MasterFragmentActivity然后将该活动扩展到DisplayFragmentsActivityTestFragmentActivity So now you will have the same drawer in your both the activities. 因此,现在您将在两个活动中使用相同的抽屉。

This will solve the problem. 这样可以解决问题。

Another suggestion you might look to avoid creating another activity, as you are using fragments try to use single activity only. 您可能希望避免创建另一个活动的另一个建议,因为您正在使用片段,请尝试仅使用单个活动。 try to find some replacement for TestFragmentActivity . 尝试找到一些替代品TestFragmentActivity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FragmentActivity和导航抽屉 - android - FragmentActivity and Navigation Drawer 在FragmentActivity中实现导航抽屉 - Implement Navigation Drawer in FragmentActivity 无法从导航抽屉中的Fragment / FragmentActivity打开FragmentActivity - Can't open FragmentActivity from Fragment/FragmentActivity in Navigation Drawer 导航抽屉和滑动选项卡:碎片和碎片活动问题 - Navigation drawer and swipe tabs: problems with fragment and fragmentactivity 使用 Fragment 和 FragmentActivity 更改导航抽屉中的活动 - Change activity in a navigation Drawer with Fragment and FragmentActivity 导航抽屉的用法-在滑动菜单上使用不同类型的组件(Activity,FragmentActivity,ListActivity,ListFragment) - Usage of Navigation Drawer - Using different Type of components(Activity,FragmentActivity,ListActivity,ListFragment) on Sliding Menu 如何从导航抽屉项中调用带有三个选项卡的包含Viewpager的FragmentActivity? - How can I able to call FragmentActivity containing viewpager with three tabs from Navigation Drawer Item? 组织FragmentActivity导航堆栈 - Organize FragmentActivity navigation stack 导航抽屉 - Navigation Drawer 带有导航抽屉片段的导航抽屉 - Navigation drawer with navigation drawer fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM