【问题标题】:Kotlin, how can I switch from one fragment to another by clicking a button? [duplicate]Kotlin,如何通过单击按钮从一个片段切换到另一个片段? [复制]
【发布时间】:2022-01-20 10:17:49
【问题描述】:

我有两个片段,我想通过单击按钮从一个片段切换到另一个片段,尽可能简单。

按钮的Id是'fragmentOneButton',我想知道如何通过按钮上的setOnClickListener移动到下一个片段。

【问题讨论】:

    标签: android kotlin android-fragments


    【解决方案1】:

    使用替换在片段之间进行更改。看看here

    transaction.replace(R.id.fragment_layout_id, fragment)
    

    或者这也是一种方式:

    var fragment: Fragment? = null
    
            when (itemId) {
                        R.id.fragment_information -> {
                            fragment = ComplainFragment()
                        }
            }
    
            if (fragment != null) {
                    val transaction = supportFragmentManager.beginTransaction()
                    transaction.replace(R.id.fragment_layout_id, fragment)
                    transaction.commit()
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      相关资源
      最近更新 更多