【问题标题】:Kotlin hide appbarlayout inside fragmentKotlin 在片段中隐藏 appbarlayout
【发布时间】:2021-06-21 10:47:10
【问题描述】:

我有 1 个活动和 4 个附加到它的片段。我的活动中有一个 appbarlayout。我的目标是在某些片段中隐藏这个 appbarlayout。可能吗?我应该怎么写代码?

活动.kt

val navController = this.findNavController(R.id.newsNavHostFragment)
        NavigationUI.setupActionBarWithNavController(this, navController)
        bottomNavigationView.setupWithNavController(newsNavHostFragment.findNavController())

【问题讨论】:

  • navController 中有一个 onDestinationChange 监听器。您可以使用它并检查当前目的地是否与您不需要 appBarLayout 的目的地相同,并且可以更新可见性。
  • 你能写一个示例代码块吗?

标签: android kotlin android-fragments mvvm toolbar


【解决方案1】:
navController.addOnDestinationChangedListener { _, destination, _ ->
   //if you want to handle multiple fragments use switch instead of if.
   if(destination.id == R.id.full_screen_destination) {
        //current Fragment is the fragment you want and do what ever you want with the appBar 
   } else {
       //current Fragment is not the fragment you want
   }
}

【讨论】:

  • 我可以再问一个问题吗?我已将导航控制器连接到底部导航视图。但是,我无法访问此结构中的其他片段。我怎样才能访问它们?
  • 你能解释一下其他片段吗?
猜你喜欢
  • 2017-11-20
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2023-04-10
  • 2019-06-27
相关资源
最近更新 更多