Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

java - How to change fragments using Android navigation drawer

I know these types of question have already been here but still I have not found my answer for this question:

  • I have created an application and used navigation drawer that has been created AUTOMATICALLY by the app (AndroidStudio)

Here's what I have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();
}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:

            break;
        case 2:

            break;
        case 3:

            break;
    }
}

And some more here:

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

I want to display another fragment using the button in navigation drawer. I want to use this code so please do not send me any guides or tutorials making their own drawers..

The question is, what do I put in case 1: case 2: and case 3: in case I want to open another fragment? Thanks.

One more question:

  • How do I add more fragments and transactions? This doesn't work-

    Fragment fragment = new MyFragment1();
    Fragment frag = new MyFragment2();
    FragmentManager fragmentManager = getFragmentManager();
    switch(position) {
        case 0:
            fragment = new MyFragment1();
            break;
        case 1:
            frag = new MyFragment2();
    
            break;
    }
    fragmentManager.beginTransaction()
            .replace(R.id.container, fragment).commit();
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should just put a switch statement into the onNavigationDrawerItemSelected method.

Something like this should work:

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    Fragment fragment;
    FragmentManager fragmentManager = getFragmentManager(); // For AppCompat use getSupportFragmentManager
    switch(position) {
        default:
        case 0:
            fragment = new MyFragment1();
            break;
        case 1:
            fragment = new MyFragment2();
            break;
    }
    fragmentManager.beginTransaction()
        .replace(R.id.container, fragment)
        .commit();
}

This is just done quickly but I think it should work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...