【问题标题】:How to pass data between fragments when using bottomNavView使用bottomNavView时如何在片段之间传递数据
【发布时间】:2020-05-03 14:17:37
【问题描述】:

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

在我的“主要”片段中,我有一个在 viewModel 中触发功能的按钮:

   edit_text_button.setOnClickListener {
            homeViewModel.onButtonClicked()
    }

onButtonClickedviewModel 内,基本上是打乱列表并在主片段中触发观察者。

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

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

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

更新 - 布局代码

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

【问题讨论】:

  • 您正在使用导航组件。使用安全参数传递数据。查看此链接 - developer.android.com/jetpack/androidx/releases/…
  • 是的,我知道如何使用 sagrArgs,但是菜单项的“问题”我不调用直接操作来导航到它们
  • 请添加代码以使事情更清楚。
  • 我更新了布局代码,如果您需要更多信息,请告诉我
  • 您可以使用navView.setOnNavigationItemSelectedListenernavController 使用参数导航到目的地。

标签: android android-fragments kotlin android-jetpack-navigation


【解决方案1】:

你可以使用

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

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

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

【讨论】:

  • 这是正确的答案,但我建议在几乎所有情况下都首选导航图范围(除非某些东西确实是应用程序全局的)。如果 ViewModel 接收到 SavedStateHandle(如果 ViewModel 仅将 SavedStateHandle 作为构造函数参数,则这是默认设置)。
【解决方案2】:

一种方法是将DestinationChangeListener 附加到您的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)

【讨论】:

    猜你喜欢
    • 2011-07-08
    • 2020-12-08
    • 1970-01-01
    • 2015-08-21
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多