简体   繁体   中英

Navigation Drawer and FragmentActivity

I'm relatively new to android development. Basically, I got a Navigation Drawer. One of the menu will need to open FragmentActivity with viewpager.

here the displayview function

 /**
 * 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. How can I display the FragmentActivity with the Navigation Drawer?

Any help would be greatly appreciated.

EDIT

my 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.

So, I suggest move your code of NavigationDrawer from DisplayFragmentsActivity to a new MasterFragmentActivity and extend that activity to your DisplayFragmentsActivity and TestFragmentActivity . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM