使用bottomNavView时如何在片段之间传递数据 [英] How to pass data between fragments when using bottomNavView

查看:432
本文介绍了使用bottomNavView时如何在片段之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个活动和两个片段的应用程序,并且我使用jetpack导航进行导航.

I have an app with one activity and two fragments, And I navigate using jetpack navigation.

在我的"Main"片段中,我有一个按钮来触发viewModel内部的函数:

In my "Main" fragment, I've a button that trigger function inside the viewModel:

   edit_text_button.setOnClickListener {
            homeViewModel.onButtonClicked()
    }

viewModel 中的

onButtonClicked ,基本上是随机播放列表并触发主片段中的观察者.

onButtonClicked inside viewModel, basically shuffle the list and trigger observer in main fragment.

   fun onButtonClicked() {
        initList = (1..9).shuffled()
        _list.value = initList
    }

我的问题是:每次将更新列表传递到底部导航栏中的第二个片段时,该如何传递?

My question is: How can I pass every time the updated list to the second fragment in my bottom nav bar?

例如,如果我的列表是[4,5,6],我希望另一个片段内的列表是[4,5,6]

For example if my list is [4,5,6], I want that the list inside the other fragment will be [4,5,6] etc

更新-布局代码

MainActivity.xml

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    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/bottom_nav_menu" />

导航

<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/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.example.sampleproject.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.example.sampleproject.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

</navigation>

菜单项(底部导航视图)

<menu xmlns:android="http://schemas.android.com/apk/res/android">

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

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />
</menu>

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    val navController = findNavController(R.id.nav_host_fragment)

    val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.navigation_home, R.id.navigation_dashboard))
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}

推荐答案

您可以使用

  • 活动范围(val viewModel: YourViewModel by activityViewModels())

导航图范围(val viewModel: YourViewModel by navGraphViewModels(R.id.desired_graph))

从该范围内的不同片段访问所需数据.

to access desired data from different fragments in that scope.

这篇关于使用bottomNavView时如何在片段之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆