10

Navigation+BottomNavigationView实现的tab效果重建fragment问题

 4 years ago
source link: https://blog.csdn.net/ljphhj/article/details/119901536
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Navigation+BottomNavigationView实现的tab效果重建fragment问题

胖虎 2021-08-25 00:14:41 8

Navigation+BottomNavigationView实现的tab效果重建fragment问题

由于这个控件内部实现会导致fragment频繁重建。因此我们需要对它的listener做一点定制,来处理这个问题


    val periodManagerFragment: Fragment = PeriodManagerFragment()
    val mineFragment: Fragment = MineFragment()

private fun initNavigationView() {
        val navView = uiViewBinding.navView
        navView.setBackgroundColor(resources.getColor(R.color.colorBottomNavigationViewBackgroundColor))
        val navController = findNavController(R.id.nav_host_fragment)
        navView.setupWithNavController(navController)
        var active = periodManagerFragment
        supportFragmentManager.beginTransaction().add(R.id.nav_host_fragment, mineFragment, "2")
            .hide(mineFragment).commit()
        supportFragmentManager.beginTransaction()
            .add(R.id.nav_host_fragment, periodManagerFragment, "1").commit()
        navView.setOnNavigationItemReselectedListener { }
        navView.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.navigation_home -> {
                    supportFragmentManager.beginTransaction().hide(active)
                        .show(periodManagerFragment).commit()
                    active = periodManagerFragment
                }
                R.id.navigation_mine -> {
                    supportFragmentManager.beginTransaction().hide(active).show(mineFragment)
                        .commit()
                    active = mineFragment
                }
            }
            return@setOnNavigationItemSelectedListener true
        }
    }

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK