【问题标题】:How can I switch from a fragment to another fragment in Kotlin?如何在 Kotlin 中从一个片段切换到另一个片段?
【发布时间】:2022-01-04 18:48:13
【问题描述】:

我希望在单击 fragment_mudur_home.xml 中的 Yetki_modulu_cardView 时打开 fragment_yetki.xml 文件。如何在 MudurHomeFragment.kt 类中执行此操作?

MudurHomeFragment.kt

fragment_home_mudur

【问题讨论】:

标签: 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2022-01-20
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    相关资源
    最近更新 更多