【问题标题】:How can I switch from a fragment to another fragment in Kotlin?如何在 Kotlin 中从一个片段切换到另一个片段?
【发布时间】:2022-01-04 18:48:13
【问题描述】:
【问题讨论】:
标签:
android
android-studio
kotlin
【解决方案1】:
首先,您必须在 mobile_navigation 页面中创建一个操作。
像这样
`<fragment
android:id="@+id/titleFragment"
android:name="com.dheeraj.guessbro.title.TitleFragment"
android:label="title_fragment"
tools:layout="@layout/title_fragment">
**<action
android:id="@+id/action_titleFragment_to_gameFragment"
app:destination="@id/gameFragment" />**
</fragment>'
然后转到 MudurHomeFragment.kt 文件并使用以下代码:
binding.yetki_modulu_cardView.setOnClickListener { findNavController(this).navigate(R.id.action_titleFragment_to_gameFragment)
}
如果您不使用视图绑定,则可以先使用 findViewById 定义变量,然后使用 clickListener。