FragmentStatePagerAdapter 已从 API 27 中弃用 [英] FragmentStatePagerAdapter is deprecated from API 27

查看:603
本文介绍了FragmentStatePagerAdapter 已从 API 27 中弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FragmentStatePagerAdapter 已从 API 27 中弃用.FragmentStatePagerAdapter 的替代方案是什么?

FragmentStatePagerAdapter is deprecated from API 27. What's the alternative of 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;
    }

}

以上代码显示 FragmentStatePagerAdaptergetItem(...)super(...) 已弃用.

Above code shows FragmentStatePagerAdapter, getItem(...) and super(...) as deprecated.

推荐答案

切换到 ViewPager2 并改用 FragmentStateAdapter.

从那里您可以使用 onPause 和 onResume 回调来确定用户当前可见的片段.当片段变得可见时调用 onResume ,当它停止可见时调用 onPause .->阅读更多内容

From there you can use onPause and onResume callbacks to determine which fragment is currently visible for the user. onResume is called when a fragment became visible and onPause when it stops to be visible. -> read more

基于 EricReejesh.

旧答案(现在也已弃用)

下面的构造函数做同样的事情

The following constructors do the same

super(@NonNull FragmentManager fm)
super(@NonNull FragmentManager fm, BEHAVIOR_SET_USER_VISIBLE_HINT)

传递BEHAVIOR_SET_USER_VISIBLE_HINT 已被弃用.您应该通过 BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT 代替.

Passing BEHAVIOR_SET_USER_VISIBLE_HINT got deprecated. You should pass BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT instead.

传递这些的区别在 FragmentPagerAdapter 中解释:

The difference in passing those is explained in 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;

这篇关于FragmentStatePagerAdapter 已从 API 27 中弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆