Reputation: 43
First of all I'm new to android developement and also to java.
I created a new project with the fragment navigation drawer, but I don't really know how to add and change to those new fragments with it. (It also seems like it doesnt change any fragments, just sets the TextView to a the selected position.)
Do you guys may know a good tutorial on how to do that (step by step)? Or can you please give me a hint on how to do it?
EDIT:
I found a solution by myself:
@Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch(position){
case 0:
fragment = new Fragment_main().newInstance(position+1);
break;
case 1:
fragment = new Fragment_two().newInstance(position+1);
break;
default:
fragment = new Fragment_main().newInstance(position +1);
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
you just have to add a switch into this method, and also create those classes (and layouts).
I hope this solution helps everyone who also struggles as begginer or with the navigation drawer :)
Upvotes: 0
Views: 1606
Reputation: 43
I found a solution by myself:
@Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch(position){
case 0:
fragment = new Fragment_main().newInstance(position+1);
break;
case 1:
fragment = new Fragment_two().newInstance(position+1);
break;
default:
fragment = new Fragment_main().newInstance(position +1);
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
you just have to add a switch into this method, and also create those classes (and layouts).
I hope this solution helps everyone who also struggles as begginer or with the navigation drawer :)
Upvotes: 3