首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Jetpack导航,带有Youtube或Instagram的BottomNavigationView,就像正确的反向导航(fragment back stack)?

Android Jetpack导航,带有Youtube或Instagram的BottomNavigationView,就像正确的反向导航(fragment back stack)?
EN

Stack Overflow用户
提问于 2018-05-29 14:06:41
回答 14查看 58.4K关注 0票数 78

安卓Jetpack导航,BottomNavigationView与自动碎片返回堆栈上的后退按钮点击?

我想要的是,在选择多个标签后,用户和用户点击后退按钮应用程序必须重定向到他/她打开的最后一个页面。

我使用Android ViewPager实现了同样的目的,将当前选中的项目保存到ArrayList中。在Android Jetpack导航发布后,有没有自动后台堆栈?我想使用导航图来实现它

activity_main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.MainActivity">

    <fragment
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

navigation.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_people"
        android:icon="@drawable/ic_group"
        android:title="@string/title_people" />

    <item
        android:id="@+id/navigation_organization"
        android:icon="@drawable/ic_organization"
        android:title="@string/title_organization" />

    <item
        android:id="@+id/navigation_business"
        android:icon="@drawable/ic_business"
        android:title="@string/title_business" />

    <item
        android:id="@+id/navigation_tasks"
        android:icon="@drawable/ic_dashboard"
        android:title="@string/title_tasks" />

</menu>

还添加了

代码语言:javascript
复制
bottomNavigation.setupWithNavController(Navigation.findNavController(this, R.id.my_nav_host_fragment))

我从Levi Moreira得到了一个答案,如下所示

代码语言:javascript
复制
navigation.setOnNavigationItemSelectedListener {item ->

            onNavDestinationSelected(item, Navigation.findNavController(this, R.id.my_nav_host_fragment))

        }

但是,这样做只会导致最后打开的片段的实例再次创建。

为BottomNavigationView提供正确的后退导航

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2021-05-22 02:47:24

随着导航包2.4.0版本的发布,它终于得到了官方的支持!

https://developer.android.com/jetpack/androidx/releases/navigation#version_240_2

不仅如此:在将导航库上传到此版本后,此功能是默认行为。顺便说一句,现在这种默认行为包括在片段之间导航时不会重新创建它们,that seemed to be something quite requested

票数 9
EN

Stack Overflow用户

发布于 2018-05-31 22:18:14

使用BottomNavigation和新的导航体系结构组件实际上并不需要ViewPager。我一直在开发一个样例应用程序,它恰好使用了这两种方法,请参阅here

基本概念是这样的,您有将托管BottomNavigationView的主要活动,这是导航图的导航主机,它的xml看起来是这样的:

activity_main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.MainActivity">

    <fragment
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

BottomNavigationView的导航菜单(选项卡菜单)如下所示:

navigation.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_people"
        android:icon="@drawable/ic_group"
        android:title="@string/title_people" />

    <item
        android:id="@+id/navigation_organization"
        android:icon="@drawable/ic_organization"
        android:title="@string/title_organization" />

    <item
        android:id="@+id/navigation_business"
        android:icon="@drawable/ic_business"
        android:title="@string/title_business" />

    <item
        android:id="@+id/navigation_tasks"
        android:icon="@drawable/ic_dashboard"
        android:title="@string/title_tasks" />

</menu>

所有这些都只是BottomNavigationView设置。现在,要使其与导航Arch组件一起工作,您需要进入导航图编辑器,添加所有片段目的地(在我的示例中,我有5个片段目的地,每个选项卡一个),并将目的地的id设置为与navigation.xml文件中的名称相同:

这将告诉android在标签和片段之间建立一个链接,现在每次用户点击“主页”标签时,android都会负责加载正确的片段。还有一段kotlin代码需要添加到您的NavHost (主活动)中,以便与BottomNavigationView连接起来

你需要在你的onCreate中添加:

代码语言:javascript
复制
bottomNavigation.setupWithNavController(Navigation.findNavController(this, R.id.my_nav_host_fragment))

这将告诉android在导航架构组件和BottomNavigationView之间进行连接。在docs中可以看到更多信息。

要获得与使用youtube时相同的效果,只需添加以下内容:

代码语言:javascript
复制
navigation.setOnNavigationItemSelectedListener {item ->

            onNavDestinationSelected(item, Navigation.findNavController(this, R.id.my_nav_host_fragment))

        }

这将使目的地进入后台堆栈,因此当您点击后退按钮时,最后访问的目的地将弹出。

票数 53
EN

Stack Overflow用户

发布于 2018-06-20 20:27:04

你必须像下面的xml一样设置主机导航:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary" />

    <fragment
        android:id="@+id/navigation_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemIconTint="@drawable/color_state_list"
        app:itemTextColor="@drawable/color_state_list"
        app:menu="@menu/menu_bottom_navigation" />
</LinearLayout>

使用导航控制器进行设置:

代码语言:javascript
复制
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_host_fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.getNavController());

menu_bottom_navigation.xml :

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@id/tab1"  // Id of navigation graph 
        android:icon="@mipmap/ic_launcher"
        android:title="@string/tab1" />
    <item
        android:id="@id/tab2" // Id of navigation graph
        android:icon="@mipmap/ic_launcher"
        android:title="@string/tab2" />

    <item
        android:id="@id/tab3" // Id of navigation graph
        android:icon="@mipmap/ic_launcher"
        android:title="@string/tab3" />
</menu>

nav_graph.xml :

代码语言:javascript
复制
<navigation 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/nav_graph"
    app:startDestination="@id/tab1">
    <fragment
        android:id="@+id/tab1"
        android:name="com.navigationsample.Tab1Fragment"
        android:label="@string/tab1"
        tools:layout="@layout/fragment_tab_1" />

    <fragment
        android:id="@+id/tab2"
        android:name="com.navigationsample.Tab2Fragment"
        android:label="@string/tab2"
        tools:layout="@layout/fragment_tab_2"/>

    <fragment
        android:id="@+id/tab3"
        android:name="com.simform.navigationsample.Tab3Fragment"
        android:label="@string/tab3"
        tools:layout="@layout/fragment_tab_3"/>
</navigation>

通过将"nav_graph“的相同id设置为"menu_bottom_navigation”,将处理底部导航的点击。

您可以使用action标记中的popUpTo属性来处理返回操作。

票数 39
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50577356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档