ViewPager2不会重复加载片段

时间:2019-12-01 06:15:08

标签: android-viewpager2

我的viewpager2没有重复加载页面。

适配器:

class ViewPager2Adapter(fragmentManager: FragmentManager, lifecycle: Lifecycle): FragmentStateAdapter(fragmentManager, lifecycle) {

    var fragments = arrayListOf(
        OneFragment(),
        TwoFragment(),
        ThreeFragment(),
        FourFragment(),
        FiveFragment(),
        SixFragment(),
        SevenFragment(),
        EightFragment()
    )
    override fun getItemCount(): Int {
        return fragments.size
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }
}

ViewPager2选项:

val adapter = ViewPager2Adapter(childFragmentManager, lifecycle)
adapter.notifyDataSetChanged()
viewpager.adapter = adapter
viewpager.orientation = ViewPager2.ORIENTATION_HORIZONTAL

在旧的viewpager片段上反复加载

0 个答案:

没有答案