我有一个包含一个 Activity 和两个片段的应用程序,然后使用jetpack导航进行导航。
在我的“Main”片段中,我有一个按钮来触发viewModel内部的函数:
edit_text_button.setOnClickListener {
homeViewModel.onButtonClicked()
}
viewModel 内的 onButtonClicked ,基本上是随机播放列表并触发主片段中的观察者。
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>
菜单项(底部导航 View )
<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)
)从该范围内的不同片段访问所需数据。
关于android - 使用bottomNavView时如何在 fragment 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61575958/