FragmentPagerAdapter deprecated(不推荐使用 FragmentPagerAdapter)
问题描述
自 API 27 起,FragmentPagerAdapter
已被弃用.最好的替代方法是什么?
Since API 27 FragmentPagerAdapter
is deprecated. What's the best alternative to use for this?
就我而言,我知道需要使用 super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
之类的东西,但我不知道在我的代码中需要去哪里.
In my case, I understand something like super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
would need to be used, but I don't know where within my code this needs to go.
我在课堂上得到了这些导入:
I got these imports in my class:
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
但是 class MyViewPagerAdapter(manager: FragmentManager) 中的
被划掉了.FragmentPagerAdapter
: FragmentPagerAdapter(manager){
but FragmentPagerAdapter
in class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager){
is crossed out.
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager){
private val fragmentList : MutableList<androidx.fragment.app.Fragment> = ArrayList()
private val titleList : MutableList<String> = 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]
}
}
推荐答案
UPDATE 2021-06-14:此时,ViewPager
本身已被弃用.从技术上讲,ViewPager
并没有被弃用,但是两个具体的 PagerAdapter
实现——FragmentPagerAdapter
和 FragmentStatePagerAdapter
——被弃用了.理想情况下,切换到其他东西,例如 ViewPager2
或 可组合的寻呼机在伴奏中.
UPDATE 2021-06-14: At this point, ViewPager
itself is all but deprecated. Technically, ViewPager
is not deprecated, but the two concrete PagerAdapter
implementations — FragmentPagerAdapter
and FragmentStatePagerAdapter
— are deprecated. Ideally, switch to something else, such as ViewPager2
or the pager composable in Accompanist.
替换:
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager)
与:
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
(假设 MyViewPagerAdapter
不需要此值即可配置)
(assuming that MyViewPagerAdapter
does not need this value to be configurable)
这篇关于不推荐使用 FragmentPagerAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不推荐使用 FragmentPagerAdapter
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01