android - 在 fragment 中隐藏底部导航 View

标签 android android-fragments kotlin android-navigation android-bottomnav

我想在一些 fragment 中隐藏bottomNavigationView。
我试过下面的代码,但它有闪烁的效果。 (bottomNavigationView 在 nextFragment 变得可见之前隐藏。

    val navController = this.findNavController(R.id.nav_host_home)
    navController.addOnDestinationChangedListener { _, destination, _ ->
    when (destination.id) {
        R.id.searchArticlesFragment -> bnvMain.visibility = View.GONE
        R.id.articleFragment -> bnvMain.visibility = View.GONE
            else -> bnvMain.visibility = View.VISIBLE
        }
    }
我也尝试了另一个代码。但它会调整 fragment 的大小。并在目标 fragment 中给出 OutOfMemoryException。
    supportFragmentManager.registerFragmentLifecycleCallbacks(object :
        FragmentManager.FragmentLifecycleCallbacks() {
        override fun onFragmentViewCreated(
            fm: FragmentManager,
            f: Fragment,
            v: View,
            savedInstanceState: Bundle?
        ) {
            when (f) {
                is SearchArticlesFragment -> bnvMain.visibility = View.GONE
                is ArticleDetailsFragment -> bnvMain.visibility = View.GONE
                else -> bnvMain.visibility = View.VISIBLE
            }
        }
    }, true)
请帮助我如何以正确和最好的方式隐藏bottomNavigationView?这是我可以隐藏bottomNavigationView的唯一方法吗? youtube 和 Instagram 是如何实现这种行为的?

最佳答案

如果您的代码遵循单一 Activity 设计模式,那么以下解决方案适合您。

  • 在父 Activity 中创建一个方法来隐藏/显示bottomNavigationView。
  • 创建一个 BaseFragment 类(通过扩展此 BaseFragment 类创建您的 fragment )
  • 在 BaseFragment 中创建一个变量来保存 bottomNavigationViewVisibility(隐藏/显示)
  • 在 BaseFragment 的 onActivityCreated 方法中,通过调用我们在 STEP1 中创建的方法,获取 Activity 引用并设置底部导航 View 可见性。
  • 在您创建的每个 fragment 中,只需设置 bottomNavigationViewVisibility 变量。

  • 例子:
    在parentAcitivty布局中,文件添加bottomNavigationView
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/main_bottom_navigation_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?android:attr/windowBackground"
                app:labelVisibilityMode="labeled"
                app:menu="@menu/main_nav" />
    
    第 1 步:在父 Activity 中,创建一个更改可见性的方法。
     fun setBottomNavigationVisibility(visibility: Int) {
            // get the reference of the bottomNavigationView and set the visibility.
            activityMainBinding.mainBottomNavigationView.visibility = visibility
        }
    
    步骤 2 & 3 & 4:
        abstract class BaseFragment : Fragment() {
        
            protected open var bottomNavigationViewVisibility = View.VISIBLE
        
            override fun onActivityCreated(savedInstanceState: Bundle?) {
                super.onActivityCreated(savedInstanceState)
                // get the reference of the parent activity and call the setBottomNavigationVisibility method.
                if (activity is MainActivity) {
                   var  mainActivity = activity as MainActivity
                    mainActivity.setBottomNavigationVisibility(bottomNavigationViewVisibility)
                }
            }
     override fun onResume() {
            super.onResume()
            if (activity is MainActivity) {
                mainActivity.setBottomNavigationVisibility(bottomNavigationViewVisibility)
            }
        }
        }
    
    第 5 步:
    class SampleFragment1 : BaseFragment() {
    
        // set the visibility here, it takes care of setting the bottomNavigationView.
        override var navigationVisibility = View.VISIBLE
        
       // override var navigationVisibility = View.GONE
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_sampleFragment1, container, false)
        }
    }
    

    关于android - 在 fragment 中隐藏底部导航 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62551658/

    相关文章:

    android - 单击 Activity 按钮时如何触发 onOptionsItemSelected(fragment)

    android - To Fragment or not to Fragment - 针对 Activity 的嵌套 fragment 。为什么我应该使用多个 Activity ?

    kotlin - 如何进行批量更新?

    Android 导航 Controller 错误 `no current navigation code`

    android - 错误 :Minifying the variant used for tests is not supported when using Jack

    android - 您如何制作 Android 应用的评论副本?

    Android Fragments 不更新

    json - 使用 Kotlinx.serialization 将 JSON 数组解析为 Map<String, String>

    android - Unity3D : Is it possible to display a running mobile app on a texture?

    java - 使用 AsyncTask 在后台创建一个 GLSurfaceView