Google Photos应用底部导航栏行为

问题描述 投票:1回答:1

很长时间以来,我一直在研究如何保存片段之间的状态,因此当我返回时,好像您从未离开过。 Google相册是一个很好的例子。滚动浏览照片,然后转到另一个片段,然后返回原样。我的问题是,由于我的应用程序加载了不同的片段,当它们返回到这些片段时,它们将被重新创建,而不是像Google Photos那样加载它们以前的状态,该如何实现。我尝试过的:


定义nav_host_fragment的部分代码,因为它是navView

 appBarConfiguration = new AppBarConfiguration.Builder(
        R.id.navigation_home, R.id.navigation_profile, R.id.navigation_locate, R.id.navigation_contact_messages, R.id.navigation_settings)
        .build();

    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    //NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);

    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    //NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);

    navView.setOnNavigationItemReselectedListener(menuItem -> {

    });

    navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
        if (destination.getLabel() != null) {
            if (levelsFragment[0].equals(destination.getLabel().toString())) {
                //reemplazar por accion
            } else if (levelsFragment[1].equals(destination.getLabel().toString())) {
                //reemplazar por accion
            } else if (levelsFragment[2].equals(destination.getLabel().toString())) {
                //reemplazar por accion
            } else if (levelsFragment[3].equals(destination.getLabel().toString())) {
                //reemplazar por accion
            } else if (levelsFragment[4].equals(destination.getLabel().toString())) {
                //reemplazar por accion
            }
        }
    });

MainActivity的

Xml,包含bottomNavigation

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    style="@style/bottomNav"
    android:layout_width="0dp"
    app:itemTextColor="@color/quantum_grey"
    android:animateLayoutChanges="true"
    app:labelVisibilityMode="auto"
    app:itemIconTint="@color/colorAccent"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="?actionBarSize"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />


</androidx.constraintlayout.widget.ConstraintLayout>

例如,在itemReselected中,我将其留空,因为每次浏览它都会为我创建一个新实例。 addOnDestinationChangedListener认为这是保存状态的地方。任何帮助,将不胜感激。这是完美解释我要达到的目标的页面:Material.io


底部导航在Android和iOS上的行为有所不同。当你选择一个底部导航项(当前未选择的一项),每个平台显示不同的结果:

  • 在Android上:该应用导航到目标的顶级屏幕。之前的所有用户互动和临时屏幕状态都会重置,例如滚动位置,标签选择和嵌入式搜索。
  • 在iOS上:目标位置反映了用户之前的互动。如果用户以前访问过该应用程序的该部分,则他们将返回查看的最后一个屏幕(如果保留,则保留其先前状态可能)。否则,该应用程序将导航到顶层屏幕。

默认平台导航可在需要改进时被覆盖用户体验。例如,一个要求在部分之间频繁切换可以保留每个部分的状态。或者,iOS应用可以使用户返回到顶级屏幕(或重置其滚动位置)(如果它更适合用例)。


这是来自Google问题跟踪器的电子邮件。明确表达了我的担忧,并且在Google相册中正确表达了我的担忧。 必须解决的问题

此错误给我们的最终用户带来的两个最大的问题和烦恼带有新的导航库和底部导航栏的是:

  • 使用底部栏更改标签页会重置上一个标签页/片段的状态和后退堆栈,因此我们的用户将完全失去其导航历史记录(在Google相册应用中正确实现了此操作)
  • 从一个选项卡的根部按回会显示初始选项卡/片段,而不是离开应用程序”

我通过搜索互联网发现了这一点,但它位于科特林,有些事情我不太了解,我不知道它是否正是我所需要的:Code in GitHub

android android-fragments navigation bottomnavigationview android-jetpack-navigation
1个回答
0
投票

我不知道是否有更干净的方法来执行此操作,但是也许您可以尝试保存重要的内容来重新创建视图(例如滚动位置,也许是内联搜索),然后在重新打开时自动滚动到旧位置。也可以保存旧实例(使用滚动数据,如果存在的话),然后在您切换回原样时,复制回旧版本而不是生成新版本。请小心复制旧屏幕对象,而不要设置引用,因为在生成下一个对象时,显然会删除该对象。.

© www.soinside.com 2019 - 2024. All rights reserved.