【发布时间】:2019-05-27 16:08:37
【问题描述】:
我在我的项目中使用底部导航活动。其中包含以下活动。我正在使用 kotlin 语言。我不确定如何在底部导航活动中从一个片段路由到另一个片段。任何帮助表示赞赏。
代码:
https://github.com/joshvapeter/KotlinBottomNavigationNew
活动
1)One Main Activity
2)Two Fragments
3)Two Adapter Class
4)Two Layout files
期待
当我单击 Fragment One Recycler View Item 时,它需要自动将其路由到下一个底部选项卡中的 Fragment 2 Recycler View Item。以下是我在项目中使用的代码。
代码
片段一适配器
itemView.setOnClickListener {
Log.d("Fragment One Clicked","Fragment One Clicked")
//FragmentTwo()
}
MainActivity
fun ShowFragmentOne() {
val transaction = manager.beginTransaction()
val fragment = FragmentOne()
transaction.replace(R.id.one, fragment)
transaction.addToBackStack(null)
transaction.commit()
isFragmentOneLoaded = true
}
fun ShowFragmentTwo() {
val transaction = manager.beginTransaction()
val fragment = FragmentTwo()
transaction.replace(R.id.two, fragment)
transaction.addToBackStack(null)
transaction.commit()
isFragmentOneLoaded = false
}
【问题讨论】:
-
最好在sn-p代码中省略与问题无关的部分,就像所有空的生命周期方法一样
-
最佳使用视图模型概念
标签: android android-studio android-fragments kotlin android-fragmentactivity