由于API 27 FragmentPagerAdapter
已弃用。最好的替代方法是什么?
就我而言,我知道super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
需要使用类似的东西,但是我不知道代码需要放在哪里。
我在课堂上得到了这些进口:
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter
但FragmentPagerAdapter
在class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager){
被划掉。
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager){ private val fragmentList : MutableList= ArrayList() private val titleList : MutableList = ArrayList() override fun getItem(position: Int): androidx.fragment.app.Fragment { return fragmentList[position] } override fun getCount(): Int { return fragmentList.size } fun addFragment(fragment: androidx.fragment.app.Fragment, title: String){ fragmentList.add(fragment) titleList.add(title) } override fun getPageTitle(position: Int): CharSequence? { return titleList[position] } }
CommonsWare.. 25
更换:
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager)
与:
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
(假设MyViewPagerAdapter
不需要此值是可配置的)
更换:
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager)
与:
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
(假设MyViewPagerAdapter
不需要此值是可配置的)