Android Fragments 使用bottomNavView时如何在片段之间传递数据

ktecyv1j  于 2023-02-08  发布在  Android
关注(0)|答案(3)|浏览(138)

我有一个只有一个活动和两个片段的应用程序,我用喷气背包导航.
在我的"Main"片段中,我有一个按钮,用于触发viewModel中的函数:

edit_text_button.setOnClickListener {
            homeViewModel.onButtonClicked()
    }
    • onButtonClicked**(在viewModel内),基本上打乱了列表并触发了主片段中的观察器。
fun onButtonClicked() {
        initList = (1..9).shuffled()
        _list.value = initList
    }

我的问题是:我怎样才能把每次更新的列表传递给底部导航栏中的第二个片段?
例如,如果我的列表是[4,5,6],我希望另一个片段中的列表是[4,5,6],等等

    • 更新-布局代码**
    • 主要活动. 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>
    • 主要活动. 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)
}
w6lpcovy

w6lpcovy1#

您可以使用

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

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

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

nwlqm0z1

nwlqm0z12#

一种方法是将DestinationChangeListener附加到您的NavController,然后每当目的地相对于navController发生变化时,都会调用侦听器,您可以比较目的地ID并将参数传递给目的地。
如下

private val onDestinationChangeListener =
        NavController.OnDestinationChangedListener { controller, destination, arguments ->
           if(destination.id == <id you want to add args>){
                destination.addArgument("Mata", NavArgument.Builder().setDefaultValue(arrayOf(1,2,3,4)).build())
            }
        }

navController.addOnDestinationChangedListener(onDestinationChangeListener)
huwehgph

huwehgph3#

我用这个代码解决了

navController.addOnDestinationChangedListener{_,_,arguments ->
                arguments?.putParcelable(DEVICE_PARAMS,deviceArgument)
            }

相关问题