Fragment Drawerlayout
I am new to android and I have implemented android studios drawerlayout. I designed the main page of my app in content_main. But now I want to switch pages by using the DrawerLayou
Solution 1:
Put this layout in your activity_main
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and to add different fragment
publicvoidaddYourFragment(){
YourFragmentmyFragment=newYourFragment();
FragmentManagerfragmentManager=this.getSupportFragmentManager();
FragmentTransactionfragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, myFragment, tagToUniqlyIdentifiedFramgent);
fragmentTransaction.addToBackStack(tagToUniqlyIdentifiedFramgent);
fragmentTransaction.commit();
}
@OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem item) {
intid= item.getItemId();
Intent nextIntent;
switch (id){
case R.id.item1:
addYourFragment();
break;
case R.id.item2:
addYourFragmentTwo()
break;
case R.id.item3:
addYourFragmentThree()
break;
}
drawer.closeDrawer(GravityCompat.START);
returntrue;
}
Post a Comment for "Fragment Drawerlayout"