使用NavigationView进行片段导航

时间:2015-06-29 23:06:18

标签: android android-fragments

我有导航视图

private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
            this, mDrawerLayout, mToolbar,
            R.string.open, R.string.close
    );
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerToggle.syncState();

    NavigationView nv = (NavigationView) findViewById(R.id.kdroid_nvView);
    nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = null;
            try {
                fragment = FragOne.class.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            ft.replace(R.id.kdroid_drawer_content, fragment);
            ft.addToBackStack(null);
            ft.commit();
            mDrawerLayout.closeDrawer(GravityCompat.START);
            return true;
        }
    });
}

FragOne是:

private Button bt;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_frag_one, null);
    bt = (Button) view.findViewById(R.id.button);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = null;
            try {
                fragment = Frag2.class.newInstance();
            } catch (java.lang.InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            ft.replace(R.id.kdroid_drawer_content, fragment);
            ft.addToBackStack(null);
            ft.commit();
        }
    });
    return view;
}

Frag2是

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_frag2, container, false);
}

我的activity_main.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/toolbar_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/kdroid_drawer_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/kdroid_nvView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/menu_drawer" />

我的toolbar_custom:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"></android.support.v7.widget.Toolbar>

所以,当我点击&#34; Frag 1&#34;在抽屉上我的应用程序呈现FragOne。在此之后,我点击&#34;按钮&#34;在FragOne上,我的应用程序呈现了Frag2。

这是一个问题!渲染frag2时,汉堡包没有变化。

This is screenshot from FragOne, when I click in NavigationView

This is screenshot from Frag2, when I click in button on FragOne, see, no have "back icon"

1 个答案:

答案 0 :(得分:0)

我的解决方案是:

  ActionBar actionBar = getSupportActionBar();
    int i = getSupportFragmentManager().getBackStackEntryCount();
    if (i > 0) {
        actionBar.setDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                change();
                onBackPressed();
            }
        });
        actionBar.setDisplayHomeAsUpEnabled(true);
    } else {
        mDrawerToggle.setDrawerIndicatorEnabled(true);
    }
    mDrawerToggle.syncState();