-2

I'm kinda new into android programming, and I couldn't find the answer on the internet. I have set up a Navigation drawer in Android Studio, and I want to change the content view if that section is selected, for example, if I select a view "Tools" in the navigation drawer, I want that you can see the layout "Tools" but that you also can go back to the navigation drawer. I have tried to put setContentView into the case 1, etc but that gave an error.

I have tried this code for the setContentView:

public class MainActivity extends Activity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

@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:
            //This is where I thought I could implement the setContentView

            setContentView(R.layout.activity_tools);
            mTitle = "Tools";
            break;
        case 2:
            mTitle = "Weapons";
            break;
        case 3:
            mTitle = "Mobs";
            break;
        case 4:
            mTitle = "Food";
            break;
        case 5:
            mTitle = "Blocks";
            break;
    }
}
Rogier
  • 1
  • 2

1 Answers1

0

ContentView is not what you want to change on drawer item selection. ContentView sets your activity layout (the one which holds NavigationDrawer view). What you really want is to replace fragments.

Lets say you have a different fragment with its own layout for every item in the navigation menu. In this case you could implement something like:

public void onNavigationDrawerItemSelected(int position) {
    Fragment fragment;
    Bundle args = new Bundle();
    String tag;
    switch (position) {
        case 1:
            fragment = new FragmentTools();
            tag = "Tools";
            args.putString(KEY_FRAGMENT_TITLE, tag);
            break;
        case 2:
            fragment = new FragmentWeapons();
            tag = "Weapons";
            args.putString(KEY_FRAGMENT_TITLE, tag);
            break;
        case 3:
            ...
    }
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    fragment.setArguments(args);
    transaction.replace(R.id.fragment_container, fragment, tag);
    transaction.commit();
}

The above is just an example. You select which fragment to show and set its arguments. Of cource it could be the same fragment with different layouts. In that case you could pass layout id as an argument, or select appropriate layout in Fragment itself based on its title.

Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72