Collapsing Toolbar only for one Fragment in Navigation View

前端 未结 5 1027
天命终不由人
天命终不由人 2021-01-02 03:47

The Problem

I have a navigation drawer with different fragments. There is a default toolbar every Fragment should use, except of one Fragment

5条回答
  •  灰色年华
    2021-01-02 04:26

    I'm using Jetpack's Navigation components with single Activity and different Fragments in my app.

    Some Fragments are accessible from bottom navigation (and have Toolbar from Activity). Some others are "special" Fragments and have their own Collapsible Toolbar.

    To achieve this, I'm hiding Toolbar from Activity in "special" Fragments with this code in Activity:

    // Handle toolbar changes in different Fragments
    val navController = findNavController(R.id.nav_host_fragment)
    navController.addOnDestinationChangedListener { _, destination, _ ->
        when (destination.id) {
            R.id.my_special_fragment_with_collapsible_toolbar -> {
                binding.toolbarMain.visibility = View.GONE
            }
            else -> {
                binding.toolbarMain.visibility = View.VISIBLE
            }
        }
    }
    

提交回复
热议问题