Reputation: 75
I was working my way through the NavigationDrawer sample code from developer.android.com and tried to implement it as navigation for one of my apps. However, I came across a problem when it came to switching fragments in the main window, as the sample code reuses the same fragment and just switches out an imageview. Here is the code from the sample:
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
where PlanetFragment is a generic fragment with only an imageview and no functionality, which does not apply for my app. So, instead, I tried and replaced the first block of the selectItem method with a case/switch:
public void selectItem(int position){
Fragment fragment;
switch(position){
case 1: fragment = new Fragmentscreentest(); break;
case 2: fragment = new Fragmentstatusbar(); break;
case 3: fragment = new Fragmentfullscreen(); break;
default: fragment = new Fragmentscreentest(); break;
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
and added the same empty constuctor to my Fragments as the PlanetFragment has:
public Fragmentstatus() {
// Empty constructor required for fragment subclasses
}
It doesn't work, whenever I open up the app, it only shows the app icon and the title in the actionbar, no content, no actions on the actionbar, no navigation drawer, nothing and then it crashes after about a second. With the original code it works and shows everything, but that only works for one fragment.
How can I actually switch Fragments in the NavigationDrawer?
Upvotes: 1
Views: 5590
Reputation: 1999
Take a look at my code:
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
switch (position) {
case 0:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new MainActivityFragment())
.commit();
break;
case 1:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new DbListViewFragment())
.commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new StatisticsFragment())
.commit();
break;
case 3:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new CalculatorFragment(), "calculator")
.commit();
break;
}
I am using a switch-case. position 0 is the very first item in my ListView inside the drawer.
Taken from my answer on this question: DrawerLayout on click disabled after first event
EDIT:
If you want to select one Fragment at start you can do it like this at the end of your onCreate method:
if (savedInstanceState == null) {
selectItem(0);
}
0 being the position of your desired Fragment
Upvotes: 7