Android - Fragment 内嵌 ViewPager ,ViewPager各个页面使用Fragment第二次进入空白界

直接说解决办法,在activity中创建adapter时fragment管理参数用supportFragmentManager,而在fragment中创建adpater时参数用childFragmentManager分开管理,就可以了。错误原因:我在fragmetn中创建adpater用了activity.supportFragmentManager,自作聪明了。

val adapter = FragmentAdapter(supportFragmentManager)
            adapter.addFragment(StationPowerFragment(it), getString(R.string.station_power))
            adapter.addFragment(StationElectricFragment(it.id!!), getString(R.string.station_electric))
            adapter.addFragment(StationLayoutFragment(it), getString(R.string.station_layout))
            adapter.addFragment(StationStatusFragment(it.id!!), getString(R.string.station_status))
            adapter.addFragment(StationSwitchFragment(it.id!!), getString(R.string.station_switch))

//            viewBinding.contentViewPager.offscreenPageLimit = 4 //预加载的页面数量
            viewBinding.contentViewPager.adapter = adapter
val adapter: FragmentAdapter = FragmentAdapter(childFragmentManager)
        adapter.addFragment(StatusBeehiveFragment(stationId), getString(R.string.beehive))
        adapter.addFragment(StatusSwarmFragment(stationId), getString(R.string.swarm))
        adapter.addFragment(StatusHoneybeeFragment(stationId), getString(R.string.honeybee))

        viewBinding.viewPager.offscreenPageLimit = 2 //缓存页面个数
        viewBinding.viewPager.adapter = adapter

你可能感兴趣的:(android)