Android Switch Fragments In The New Design Support Navigation Drawer
how can I switch Fragments in the new design support navigation drawer? I found example codes on the Cheesesquare Github on how to switch fragments using the TabLayout, but not the
Solution 1:
Write some code like this:
navigationView.setNavigationItemSelectedListener(
newNavigationView.OnNavigationItemSelectedListener() {
@OverridepublicbooleanonNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.your_menu_id:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, getFragment(), "SET_A_TAG").addToBackStack("SET_A_TAG").commit();
break;
}
returntrue;
}
});
privateYourFragmentgetFragment() {
YourFragment f = getSupportFragmentManager().findFragmentByTag("SET_A_TAG");
if (f == null) {
f = newYourFragment();
}
return f;
}
Solution 2:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/drawer_layout"android:layout_height="match_parent"android:layout_width="match_parent"android:fitsSystemWindows="true">
<include layout="@layout/include_list_viewpager"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@id/fragmentContainer"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view"/>
Something like this?
Post a Comment for "Android Switch Fragments In The New Design Support Navigation Drawer"