首页 > 解决方案 > 在片段错误之间导航(BottomNavigationView Visibility)

问题描述

我试图使片段A中的BottomNavigationView 不可见,但在切换到片段B时可见,我尝试在整个导航过程中记录它们的生命周期并尝试visiblity相应地设置但仍然无济于事......

我正在使用在Navigation片段之间导航,导航是在adapter传递onClick视图的片段中完成的

编辑:我尝试使用onDestinationListener

错误:它随机工作

片段A:

lateinit var bvn: BottomNavigationView

val TAG = "TAG"


override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_post_detail, container, false)

    Log.d(TAG, "PDF: onCreateView")
val navController = findNavController(requireActivity(),R.id.nav_host_fragment) 
      navController.addOnDestinationChangedListener { _, destination, _ ->
        if(destination.id == R.id.postDetailFragment) {
            bnv.visibility = View.GONE
        } else {
            bnv.visibility = View.VISIBLE
        }
    }

    ...
    ...


    return view
}

片段B:

lateinit var bvn: BottomNavigationView

val TAG = "TAG"

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_profile, container, false)


    Log.d(TAG, "Profile: onCreateView")

   val navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
    navController.addOnDestinationChangedListener { _, destination, _ ->
        if(destination.id == R.id.nav_profile) {
            bnv.visibility = View.VISIBLE
            Log.d(TAG, "Profile: bnv visible")
        }
    }
   ...
   ...

    return view
}

片段A适配器:

  ...
  ...
  override fun onBindViewHolder(holder: Viewholder, position: Int) {

        // navigating to FragmentB...

        holder.imageProfile.setOnClickListener {
            val action = PostDetailFragmentDirections
                .actionPostDetailFragmentToNavProfile(comment.publisher)
            it.findNavController().navigate(action)
        }

    }

标签: androidandroid-fragmentskotlinbottomnavigationview

解决方案


推荐阅读