gpt4 book ai didi

android - 如何显示子 fragment 工具栏的后退箭头

转载 作者:行者123 更新时间:2023-12-05 07:45:29 25 4
gpt4 key购买 nike

我有一个包含很多 fragment 的主要 Activity ,主要 Activity 布局如下:

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

当我将页面切换到下一个 fragment 时,我希望该 fragment 在工具栏上有一个后退箭头,它可以返回上一个 Activity ,也许是 fragment 。

我尝试在主要 Activity 上设置功能:

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());
}
});
}

我的开关函数:

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();
}

我在下一个 fragment 中这样调用它:

    @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;
}

我可以切换到下一个 fragment ,然后先点击返回。

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

让我的子 fragment 具有后退箭头并且它可以更改为以前的布局的最佳方法是什么?

最佳答案

使用 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();

在 fragment 附加/switchFragment 完成后设置下面的代码

 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>

ma​​inLayout.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>

关于android - 如何显示子 fragment 工具栏的后退箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41592732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com