Android-Navigation-导航至其他Fragment时隐藏BottomNavigationView

添加一个显示和隐藏动画效果更好。

/**
 * BottomNav显示控制
 */
private var isShowBottomNav = true

mNavController.addOnDestinationChangedListener { _, destination, _ ->
            // 显示或隐藏导航
            when (destination.id) {
                R.id.xxxFragment, R.id.xxxFragment, R.id.xxxFragment, R.id.xxxFragment -> {
                    showBottomNav()
                }
                else -> hideBottomNav()
            }
        }

    /**
     * 显示导航
     */
    private fun showBottomNav() {
        if (!isShowBottomNav) {
            val translationY = AnimUtil.translationY(
                    binding.bottomNav, binding.bottomNav.height.toFloat(), 0F,
                    AnimUtil.DURATION_300
            )
            translationY.addListener(object : MyAnimatorListener() {
                override fun onAnimationStart(animation: Animator?) {
                    binding.bottomNav.visibility = View.VISIBLE
                }
            })
            translationY.start()
            isShowBottomNav = true
        }
    }

    /**
     * 隐藏导航
     */
    private fun hideBottomNav() {
        if (isShowBottomNav) {
            val translationY = AnimUtil.translationY(
                    binding.bottomNav, 0F, binding.bottomNav.height.toFloat(),
                    AnimUtil.DURATION_300
            )
            translationY.addListener(object : MyAnimatorListener() {
                override fun onAnimationEnd(animation: Animator?) {
                    binding.bottomNav.visibility = View.GONE
                }
            })
            translationY.start()
            isShowBottomNav = false
        }
    }

你可能感兴趣的:(Android)