android - 从 API 27 弃用 FragmentStatePagerAdapter
问题描述
FragmentStatePagerAdapter
已从 API 27 中弃用。有什么替代方法FragmentStatePagerAdapter
?
private class MainPagerAdapter extends FragmentStatePagerAdapter {
MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment result = new DummyFragment();
return result;
}
@Override
public int getCount() {
return 5;
}
}
上面的代码显示FragmentStatePagerAdapter
,getItem(...)
和super(...)
已弃用。
解决方案
切换到ViewPager2并改用FragmentStateAdapter。
从那里您可以使用 onPause 和 onResume 回调来确定当前对用户可见的片段。当一个片段变得可见时调用 onResume,当它停止可见时调用 onPause。->阅读更多
旧答案(现在也已弃用)
以下构造函数做同样的事情
super(@NonNull FragmentManager fm)
super(@NonNull FragmentManager fm, BEHAVIOR_SET_USER_VISIBLE_HINT)
传递BEHAVIOR_SET_USER_VISIBLE_HINT
已被弃用。你应该通过BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
。
通过这些的差异在以下内容中进行了解释FragmentPagerAdapter
:
/**
* Indicates that Fragment#setUserVisibleHint(boolean) will be
* called when the current fragment changes.
*/
@Deprecated
public static final int BEHAVIOR_SET_USER_VISIBLE_HINT = 0;
/**
* Indicates that only the current fragment will be
* in the Lifecycle.State#RESUMED state. All other Fragments
* are capped at Lifecycle.State#STARTED.
*/
public static final int BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT = 1;
推荐阅读
- excel - 复制和粘贴具有可变单元格范围的行失败
- python - 我无法正确导入另一个脚本 (SMTP)
- python - 二叉树解释为什么这样做
- python - 在 filtfilt 的情况下,ba 和 sos 过滤器顺序不同?
- angular - 检索 firebase 列表数据以创建动态表
- java - 当 double 作为 int 输入时,尝试输出不带小数的 double
- bash - Bash 递归脚本变得非常慢
- python - 如何在 python 代码中自动识别加密最佳实践
- python - 直接从“云”或“在线”github 工作
- apache-kafka - 禁用 Kafka 会话超时