通过单击工具栏上的后退箭头

时间:2017-03-17 12:48:50

标签: android android-fragments navigation-drawer

我正在构建的应用使用导航抽屉android.support.v4.widget.DrawerLayoutMainActivity扩展android.support.v7.app.AppCompatActivity。我有一个由子片段替换的父片段。在父片段上,默认汉堡包图标正确切换导航抽屉。在儿童片段上,使用此片段将汉堡包图标替换为后退箭头图标

((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar = (Toolbar) (getActivity()).findViewById(R.id.toolbar);
((MainActivity)getActivity()).setSupportActionBar(toolbar);

在儿童片段后退按钮上,点击事件在onOptionsItemSelected()上处理,汉堡包图标再次显示在onStop()上的父片段。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getActivity().onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onStop() {
    super.onStop();
    ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    MainActivity.toggle.syncState();
}

在父级片段汉堡包图标现在正常显示,但点击它后导航抽屉不会再显示。

有人可以帮忙解决我的错误吗?

以下是相关文件:

MainActivity.java

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
public static ActionBarDrawerToggle toggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    try {
        Fragment fragment = (Fragment) ParentFragment.class.newInstance();

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, fragment).commit();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

ParentFragment.java

public class ParentFragment extends Fragment implements View.OnClickListener {
public static ParentFragment newInstance() {
    ParentFragment fragment = new ParentFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onClick(View v) {
    int i = v.getId();
    Fragment fragment = null;

    if(i == R.id.child_fragment_button) {
        fragment = new ChildFragment();
    } 

    if(fragment != null) {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_content, fragment)
                .addToBackStack(null)
                .commit();
    }
}
}

ChildFragment.java

public class ChildFragment extends Fragment { 
private Toolbar toolbar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

    // Show back button
    ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    toolbar = (Toolbar) (getActivity()).findViewById(R.id.toolbar);
    ((MainActivity)getActivity()).setSupportActionBar(toolbar);
}

@Override
public void onStop() {
    super.onStop();
    ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    MainActivity.toggle.syncState();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getActivity().onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。感谢this answer

以下是我所做的更改:

<强> ChildFragment.java

public class ChildFragment extends Fragment { 
private Toolbar toolbar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

    // Show back button

    toggle = MainActivity.toggle;
    toggle.setDrawerIndicatorEnabled(false);
    toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager manager = getActivity().getSupportFragmentManager();
            manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

            toggle.setDrawerIndicatorEnabled(true);
        }
    });

    toolbar = (Toolbar) (getActivity()).findViewById(R.id.toolbar);
    toolbar.setNavigationIcon(((MainActivity)getActivity()).getDrawerToggleDelegate().getThemeUpIndicator());

}

@Override
public void onStop() {
    super.onStop();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        default:
            return super.onOptionsItemSelected(item);
    }
}
}