如何处理点击顶部应用栏按钮来切换片段?

问题描述 投票:0回答:1
android android-fragments button
1个回答
0
投票

是的,正确的方法是,只需添加一个函数调用即可对所需的片段执行相应的导航。

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        R.id.action_settings -> {
            settings()      // function call to navigate to required fragment
            true
        }
        R.id.action_book -> {
            book()          // function call to navigate to required fragment
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
}

fun book(){
// write navigation code here
}

fun settings(){
// write navigation code here
}
© www.soinside.com 2019 - 2024. All rights reserved.