如何在子片段工具栏中显示返回箭头

3

我有一个包含许多片段的主活动,主活动布局如下:

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


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF">

//----------------------here is my toolbar------------------------
<include layout="@layout/toolbar"/>

        <FrameLayout
            android:id="@+id/mainFrame"
            android:layout_gravity="end"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

    </android.support.design.widget.AppBarLayout>


    <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:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header">
    </android.support.design.widget.NavigationView>


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

当我切换页面到下一个片段时,我希望该片段在工具栏上有一个返回箭头,它可以返回先前的活动,或许是片段。
我尝试在主要活动中设置该功能:
public void setBackArrow() {
    toolbar.setNavigationIcon(R.drawable.nav_icon_arrow_18x18_white);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switchFragment(NewsInformation.newInstance());
        }
    });
}

我的switch函数:

private void switchFragment(Fragment fragment) {
        FragmentManager manager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.mainFrame, fragment, null);
        //transaction.addToBackStack(null);
        transaction.commit();
    }

我将其称为下一个片段,像这样调用它:

    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
      View view =inflater.inflate(R.layout.activity_list_fragment,container, false);
//try to let the fragment has a back arrow toolbar , and has the onClick to previous layout     
      ((MainActivity) getActivity()).setBackArrow();

      return view;
      }

我可以切换到下一个片段并首先单击它。

但是我的问题是当我返回到上一个布局时,我失去了工具栏菜单功能原始,void setBackArrow()替换它,并且导航图标始终为R.drawable.nav_icon_arrow_18x18_white。

最好的方法是让我的子片段具有返回箭头并且可以更改到先前的布局吗?

1个回答

0

使用AppCompatActivity代替Activity

以下步骤描述了整体思路。

onCreate()

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

在片段附加/切换片段完成后设置以下代码

 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
 drawer.closeDrawer(GravityCompat.START);  

onBackButtonHit()

 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
 if (drawer.isDrawerOpen(GravityCompat.START)) {
      drawer.closeDrawer(GravityCompat.START);
  }   

string.xml

<resources>
    <string name="navigation_drawer_open">Open navigation drawer</string>
    <string name="navigation_drawer_close">Close navigation drawer</string>
</resources>

mainLayout.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
       <!-- AppBarLayout -->
       <!-- Fragment holer etc..--> />

    <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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接