ViewPager2的正确使用姿势

一、使用方式

ViewPager2是什么我就不说了 我先来三件套(Activity Fragment Adapter)让大家看看到底是怎么使用的,具体的布局文件我就不贴了很简单布局

public class ViewPager2Activity extends AppCompatActivity {

    ViewPager2 viewpager2;
    ViewPager2Adapter viewPager2Adapter;
    List<ViewPagerFragment> viewPagerFragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager2);
        viewpager2 = findViewById(R.id.viewpager2);
        viewPagerFragments = new ArrayList<ViewPagerFragment>();
        for (int i = 0; i < 10; i++) {
            viewPagerFragments.add(ViewPagerFragment.getInstance(Integer.toString(i)));
        }
        viewPager2Adapter = new ViewPager2Adapter(this, viewPagerFragments);
        viewpager2.setAdapter(viewPager2Adapter);
        viewpager2.setCurrentItem(3);
        viewpager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                Log.d("viewpager", "onPageScrolled 页面开始滚动 " + position);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Log.d("viewpager", "onPageSelected 页面被选中 " + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                Log.d("viewpager", "onPageScrollStateChanged 页面在滚动 " + state);
            }
        });
    }
}
public class ViewPagerFragment extends Fragment {


    private static final String TAG = "ViewPagerFragment";
    String mName;

    public static ViewPagerFragment getInstance(String name) {
        ViewPagerFragment viewPagerFragment = new ViewPagerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        viewPagerFragment.setArguments(bundle);
        return viewPagerFragment;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach frag=" + this.toString());
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate frag=" + this.toString());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        Log.d(TAG, "onCreateView frag=" + this.toString());
        View view = inflater.inflate(R.layout.fragment_viewpager2, null);
        TextView tv = view.findViewById(R.id.tv1);

        Bundle bundle = getArguments();
        if (bundle != null) {
            mName = bundle.getString("name");
        }
        tv.setText("这是第 "+mName+" fragment");
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG, "onActivityCreated frag=" + this.toString());
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart frag=" + this.toString());
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume frag=" + this.toString());
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause " + this.toString());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy " + this.toString());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG, "onDetach " + this.toString());
    }
}
public class ViewPager2Adapter extends FragmentStateAdapter {

    List<ViewPagerFragment> mdatas;

    public ViewPager2Adapter(@NonNull FragmentActivity fragmentActivity, List<ViewPagerFragment> fragments) {
        super(fragmentActivity);
        this.mdatas = fragments;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return mdatas.get(position);
    }

    @Override
    public int getItemCount() {
        return mdatas.size();
    }
}

注意 使用list<Fragment> 的话 一定要给viewpager2设置缓存 否则会出现
Fragment no longer exists for key f0: unique id 错误

为了解决这个问题 可以使用以下的adapter 这样就不会出错

class AdapterFragmentPager(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {

  override fun createFragment(position: Int): Fragment {
    return when (position) {
      PAGE_HOME -> HomeFragment.getInstance();
      PAGE_FIND -> PageFragment.getInstance();
      PAGE_INDICATOR -> IndicatorFragment.getInstance();
      PAGE_OTHERS -> OthersFragment.getInstance();
      else -> EmptyFragment.getInstance();
    }
  }

  override fun getItemCount(): Int {
    return 4
  }

  companion object {
    const val PAGE_HOME = 0
    const val PAGE_FIND = 1
    const val PAGE_INDICATOR = 2
    const val PAGE_OTHERS = 3
  }
}

看看 viewpager2.setCurrentItem(3); 这个地方 我们直接跳转到索引为3的Fragment里面看看log日志

2021-05-28 18:21:25.240 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{3dfbf6b (5586edc8-3823-4f87-b1a1-dc95831f57e3) f0}
2021-05-28 18:21:25.241 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{3dfbf6b (5586edc8-3823-4f87-b1a1-dc95831f57e3) f0}
2021-05-28 18:21:25.241 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{3dfbf6b (5586edc8-3823-4f87-b1a1-dc95831f57e3) f0}
2021-05-28 18:21:25.248 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{3dfbf6b (5586edc8-3823-4f87-b1a1-dc95831f57e3) f0}
2021-05-28 18:21:25.248 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{3dfbf6b (5586edc8-3823-4f87-b1a1-dc95831f57e3) f0}
2021-05-28 18:21:25.253 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{953199 (1f575c8a-207f-4cb6-82f7-b92c27f30032) f1}
2021-05-28 18:21:25.253 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{953199 (1f575c8a-207f-4cb6-82f7-b92c27f30032) f1}
2021-05-28 18:21:25.253 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{953199 (1f575c8a-207f-4cb6-82f7-b92c27f30032) f1}
2021-05-28 18:21:25.257 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{953199 (1f575c8a-207f-4cb6-82f7-b92c27f30032) f1}
2021-05-28 18:21:25.258 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{953199 (1f575c8a-207f-4cb6-82f7-b92c27f30032) f1}
2021-05-28 18:21:25.281 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{ea27ad1 (f8883f88-2bd8-4bd1-86ab-38cb9c9fe88b) f2}
2021-05-28 18:21:25.281 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{ea27ad1 (f8883f88-2bd8-4bd1-86ab-38cb9c9fe88b) f2}
2021-05-28 18:21:25.281 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{ea27ad1 (f8883f88-2bd8-4bd1-86ab-38cb9c9fe88b) f2}
2021-05-28 18:21:25.286 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{ea27ad1 (f8883f88-2bd8-4bd1-86ab-38cb9c9fe88b) f2}
2021-05-28 18:21:25.286 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{ea27ad1 (f8883f88-2bd8-4bd1-86ab-38cb9c9fe88b) f2}
2021-05-28 18:21:25.365 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{e78b309 (fb6a439c-3100-47ba-b376-d00cc177b843) f3}
2021-05-28 18:21:25.366 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{e78b309 (fb6a439c-3100-47ba-b376-d00cc177b843) f3}
2021-05-28 18:21:25.366 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{e78b309 (fb6a439c-3100-47ba-b376-d00cc177b843) f3}
2021-05-28 18:21:25.371 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{e78b309 (fb6a439c-3100-47ba-b376-d00cc177b843) f3}
2021-05-28 18:21:25.371 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{e78b309 (fb6a439c-3100-47ba-b376-d00cc177b843) f3}
2021-05-28 18:21:25.733 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onDestroy ViewPagerFragment{3dfbf6b (5586edc8-3823-4f87-b1a1-dc95831f57e3) f0}
2021-05-28 18:21:25.733 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onDetach ViewPagerFragment{3dfbf6b (5586edc8-3823-4f87-b1a1-dc95831f57e3) f0}
2021-05-28 18:21:25.734 10792-10792/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onResume frag=ViewPagerFragment{e78b309 (fb6a439c-3100-47ba-b376-d00cc177b843) f3}

从log上来分析 我们跳转到索引为3的Fragment ,ViewPager2会把之前的f0,f1,f2都创建出来,并且从肉眼看明显是从f0 过渡到f3的, 但是f0 执行了onDestroy 与onDetach方法 说明f0被销毁了。大概推测就是ViewPager2跳转到某个位置 最多缓存2个fragment。
为了验证自己的想法,多测试一下 把viewpager2.setCurrentItem(4); 再看看log

2021-05-28 19:05:32.913 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{ab63ef0 (38f316ba-a11a-4ad5-b108-ba68daaa1e62) f1}
2021-05-28 19:05:32.913 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{ab63ef0 (38f316ba-a11a-4ad5-b108-ba68daaa1e62) f1}
2021-05-28 19:05:32.914 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{ab63ef0 (38f316ba-a11a-4ad5-b108-ba68daaa1e62) f1}
2021-05-28 19:05:32.919 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{ab63ef0 (38f316ba-a11a-4ad5-b108-ba68daaa1e62) f1}
2021-05-28 19:05:32.919 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{ab63ef0 (38f316ba-a11a-4ad5-b108-ba68daaa1e62) f1}
2021-05-28 19:05:32.960 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{eabefa1 (6ffb2fdd-b588-4abc-b9eb-a4fb183a9bd8) f2}
2021-05-28 19:05:32.960 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{eabefa1 (6ffb2fdd-b588-4abc-b9eb-a4fb183a9bd8) f2}
2021-05-28 19:05:32.961 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{eabefa1 (6ffb2fdd-b588-4abc-b9eb-a4fb183a9bd8) f2}
2021-05-28 19:05:32.965 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{eabefa1 (6ffb2fdd-b588-4abc-b9eb-a4fb183a9bd8) f2}
2021-05-28 19:05:32.965 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{eabefa1 (6ffb2fdd-b588-4abc-b9eb-a4fb183a9bd8) f2}
2021-05-28 19:05:32.968 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{401f1d9 (01c471a2-acf8-4928-a1d8-7eb92fce16ae) f3}
2021-05-28 19:05:32.968 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{401f1d9 (01c471a2-acf8-4928-a1d8-7eb92fce16ae) f3}
2021-05-28 19:05:32.969 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{401f1d9 (01c471a2-acf8-4928-a1d8-7eb92fce16ae) f3}
2021-05-28 19:05:32.973 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{401f1d9 (01c471a2-acf8-4928-a1d8-7eb92fce16ae) f3}
2021-05-28 19:05:32.973 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{401f1d9 (01c471a2-acf8-4928-a1d8-7eb92fce16ae) f3}
2021-05-28 19:05:33.027 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{25f0311 (067ee95c-43bf-48bb-a9cd-3f564a45034c) f4}
2021-05-28 19:05:33.028 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{25f0311 (067ee95c-43bf-48bb-a9cd-3f564a45034c) f4}
2021-05-28 19:05:33.028 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{25f0311 (067ee95c-43bf-48bb-a9cd-3f564a45034c) f4}
2021-05-28 19:05:33.032 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{25f0311 (067ee95c-43bf-48bb-a9cd-3f564a45034c) f4}
2021-05-28 19:05:33.033 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{25f0311 (067ee95c-43bf-48bb-a9cd-3f564a45034c) f4}
2021-05-28 19:05:33.398 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onDestroy ViewPagerFragment{ab63ef0 (38f316ba-a11a-4ad5-b108-ba68daaa1e62) f1}
2021-05-28 19:05:33.398 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onDetach ViewPagerFragment{ab63ef0 (38f316ba-a11a-4ad5-b108-ba68daaa1e62) f1}
2021-05-28 19:05:33.399 17410-17410/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onResume frag=ViewPagerFragment{25f0311 (067ee95c-43bf-48bb-a9cd-3f564a45034c) f4}

果然是的 你们看 开始创建的fragment位置从1开始了。

二、setCurrentItem()调用总结

就是跳转的位置往前移动3位开始创建fragment,然后销毁第一个创建fragment 再显示当前位置的fragment。当然不满往前移动3位的自己可以测试一下。

三、viewpager2.setCurrentItem(positon,false)

有时候我们的业务需求不需要这种过渡动画怎么办呢?可以使用viewpager2.setCurrentItem(4,false);
传false就是直接跳转到对应fragemnt 没有过渡动画。并且我们来看一下log

2021-05-28 19:14:10.152 18927-18927/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{3dfbf6b (4722ccfe-6a59-4e32-99a0-3d4810e6754a) f4}
2021-05-28 19:14:10.152 18927-18927/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{3dfbf6b (4722ccfe-6a59-4e32-99a0-3d4810e6754a) f4}
2021-05-28 19:14:10.152 18927-18927/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{3dfbf6b (4722ccfe-6a59-4e32-99a0-3d4810e6754a) f4}
2021-05-28 19:14:10.159 18927-18927/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{3dfbf6b (4722ccfe-6a59-4e32-99a0-3d4810e6754a) f4}
2021-05-28 19:14:10.160 18927-18927/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{3dfbf6b (4722ccfe-6a59-4e32-99a0-3d4810e6754a) f4}
2021-05-28 19:14:10.160 18927-18927/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onResume frag=ViewPagerFragment{3dfbf6b (4722ccfe-6a59-4e32-99a0-3d4810e6754a) f4}

真的很神奇,居然直接从f4开始创建显示。这样的确挺爽的,不需要缓存的情况下需要那个fragemnt就创建那个fragment。

四、viewpager2.setOffscreenPageLimit()

当我把代码改成了
viewpager2.setOffscreenPageLimit(4);
viewpager2.setCurrentItem(4);
来看看log

2021-05-28 19:21:11.526 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{3dfbf6b (e82c2b25-85ae-496d-acbc-a1629d994b8d) f1}
2021-05-28 19:21:11.527 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{3dfbf6b (e82c2b25-85ae-496d-acbc-a1629d994b8d) f1}
2021-05-28 19:21:11.527 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{3dfbf6b (e82c2b25-85ae-496d-acbc-a1629d994b8d) f1}
2021-05-28 19:21:11.534 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{3dfbf6b (e82c2b25-85ae-496d-acbc-a1629d994b8d) f1}
2021-05-28 19:21:11.534 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{3dfbf6b (e82c2b25-85ae-496d-acbc-a1629d994b8d) f1}
2021-05-28 19:21:11.538 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{953199 (f775a723-2ec9-472c-b85b-80430f7802ea) f2}
2021-05-28 19:21:11.538 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{953199 (f775a723-2ec9-472c-b85b-80430f7802ea) f2}
2021-05-28 19:21:11.538 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{953199 (f775a723-2ec9-472c-b85b-80430f7802ea) f2}
2021-05-28 19:21:11.543 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{953199 (f775a723-2ec9-472c-b85b-80430f7802ea) f2}
2021-05-28 19:21:11.543 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{953199 (f775a723-2ec9-472c-b85b-80430f7802ea) f2}
2021-05-28 19:21:11.546 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{ea27ad1 (0942bc7f-3920-44b6-a6b9-f1efb56452f2) f3}
2021-05-28 19:21:11.546 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{ea27ad1 (0942bc7f-3920-44b6-a6b9-f1efb56452f2) f3}
2021-05-28 19:21:11.546 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{ea27ad1 (0942bc7f-3920-44b6-a6b9-f1efb56452f2) f3}
2021-05-28 19:21:11.551 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{ea27ad1 (0942bc7f-3920-44b6-a6b9-f1efb56452f2) f3}
2021-05-28 19:21:11.551 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{ea27ad1 (0942bc7f-3920-44b6-a6b9-f1efb56452f2) f3}
2021-05-28 19:21:11.554 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{e78b309 (b573f271-78c7-467f-ba33-2de5033a3f0c) f4}
2021-05-28 19:21:11.554 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{e78b309 (b573f271-78c7-467f-ba33-2de5033a3f0c) f4}
2021-05-28 19:21:11.554 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{e78b309 (b573f271-78c7-467f-ba33-2de5033a3f0c) f4}
2021-05-28 19:21:11.558 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{e78b309 (b573f271-78c7-467f-ba33-2de5033a3f0c) f4}
2021-05-28 19:21:11.559 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{e78b309 (b573f271-78c7-467f-ba33-2de5033a3f0c) f4}
2021-05-28 19:21:11.561 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{466ba41 (d873c7c0-7d8b-4656-b7b6-3ecbbb808a03) f5}
2021-05-28 19:21:11.562 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{466ba41 (d873c7c0-7d8b-4656-b7b6-3ecbbb808a03) f5}
2021-05-28 19:21:11.562 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{466ba41 (d873c7c0-7d8b-4656-b7b6-3ecbbb808a03) f5}
2021-05-28 19:21:11.567 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{466ba41 (d873c7c0-7d8b-4656-b7b6-3ecbbb808a03) f5}
2021-05-28 19:21:11.567 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{466ba41 (d873c7c0-7d8b-4656-b7b6-3ecbbb808a03) f5}
2021-05-28 19:21:11.570 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{3b77079 (6c747842-38f9-4577-b708-ce2c4cdf79ff) f0}
2021-05-28 19:21:11.570 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{3b77079 (6c747842-38f9-4577-b708-ce2c4cdf79ff) f0}
2021-05-28 19:21:11.571 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{3b77079 (6c747842-38f9-4577-b708-ce2c4cdf79ff) f0}
2021-05-28 19:21:11.575 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{3b77079 (6c747842-38f9-4577-b708-ce2c4cdf79ff) f0}
2021-05-28 19:21:11.575 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{3b77079 (6c747842-38f9-4577-b708-ce2c4cdf79ff) f0}
2021-05-28 19:21:11.623 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{231b5b1 (64084bb0-471a-4fa5-8f5a-dc149474b39f) f6}
2021-05-28 19:21:11.623 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{231b5b1 (64084bb0-471a-4fa5-8f5a-dc149474b39f) f6}
2021-05-28 19:21:11.624 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{231b5b1 (64084bb0-471a-4fa5-8f5a-dc149474b39f) f6}
2021-05-28 19:21:11.629 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{231b5b1 (64084bb0-471a-4fa5-8f5a-dc149474b39f) f6}
2021-05-28 19:21:11.629 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{231b5b1 (64084bb0-471a-4fa5-8f5a-dc149474b39f) f6}
2021-05-28 19:21:11.721 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{b9869e9 (949979b9-c395-493d-870f-7720ac5f9fbb) f7}
2021-05-28 19:21:11.722 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{b9869e9 (949979b9-c395-493d-870f-7720ac5f9fbb) f7}
2021-05-28 19:21:11.722 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{b9869e9 (949979b9-c395-493d-870f-7720ac5f9fbb) f7}
2021-05-28 19:21:11.726 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{b9869e9 (949979b9-c395-493d-870f-7720ac5f9fbb) f7}
2021-05-28 19:21:11.727 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{b9869e9 (949979b9-c395-493d-870f-7720ac5f9fbb) f7}
2021-05-28 19:21:11.856 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{52a6d21 (5eeeaf25-5ac4-4cf1-8c38-f3946c626454) f8}
2021-05-28 19:21:11.856 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{52a6d21 (5eeeaf25-5ac4-4cf1-8c38-f3946c626454) f8}
2021-05-28 19:21:11.856 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{52a6d21 (5eeeaf25-5ac4-4cf1-8c38-f3946c626454) f8}
2021-05-28 19:21:11.862 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{52a6d21 (5eeeaf25-5ac4-4cf1-8c38-f3946c626454) f8}
2021-05-28 19:21:11.862 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{52a6d21 (5eeeaf25-5ac4-4cf1-8c38-f3946c626454) f8}
2021-05-28 19:21:12.177 20422-20422/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onResume frag=ViewPagerFragment{e78b309 (b573f271-78c7-467f-ba33-2de5033a3f0c) f4}

从f0到f8 一共创建了9个fragment 意思从当前位置 往前创建4个 往后创建4个,这里看log 不知道为什么不是从f0开始创建有大神知道的可以告知一下。

代码再改成这样呢
viewpager2.setOffscreenPageLimit(4);
viewpager2.setCurrentItem(4,false);

2021-05-28 19:26:17.977 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{3dfbf6b (7cf4ad3c-acc3-4ad9-b11c-bec1628780ea) f4}
2021-05-28 19:26:17.977 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{3dfbf6b (7cf4ad3c-acc3-4ad9-b11c-bec1628780ea) f4}
2021-05-28 19:26:17.978 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{3dfbf6b (7cf4ad3c-acc3-4ad9-b11c-bec1628780ea) f4}
2021-05-28 19:26:17.985 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{3dfbf6b (7cf4ad3c-acc3-4ad9-b11c-bec1628780ea) f4}
2021-05-28 19:26:17.985 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{3dfbf6b (7cf4ad3c-acc3-4ad9-b11c-bec1628780ea) f4}
2021-05-28 19:26:17.986 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onResume frag=ViewPagerFragment{3dfbf6b (7cf4ad3c-acc3-4ad9-b11c-bec1628780ea) f4}
2021-05-28 19:26:17.990 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{9f8ec5e (e75f344b-5f3c-4775-877b-8e5f8b280ea8) f5}
2021-05-28 19:26:17.990 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{9f8ec5e (e75f344b-5f3c-4775-877b-8e5f8b280ea8) f5}
2021-05-28 19:26:17.990 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{9f8ec5e (e75f344b-5f3c-4775-877b-8e5f8b280ea8) f5}
2021-05-28 19:26:17.995 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{9f8ec5e (e75f344b-5f3c-4775-877b-8e5f8b280ea8) f5}
2021-05-28 19:26:17.995 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{9f8ec5e (e75f344b-5f3c-4775-877b-8e5f8b280ea8) f5}
2021-05-28 19:26:17.998 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{b402936 (9078db97-737f-42b4-a480-a41728f37554) f6}
2021-05-28 19:26:17.998 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{b402936 (9078db97-737f-42b4-a480-a41728f37554) f6}
2021-05-28 19:26:17.998 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{b402936 (9078db97-737f-42b4-a480-a41728f37554) f6}
2021-05-28 19:26:18.002 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{b402936 (9078db97-737f-42b4-a480-a41728f37554) f6}
2021-05-28 19:26:18.002 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{b402936 (9078db97-737f-42b4-a480-a41728f37554) f6}
2021-05-28 19:26:18.005 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{1e7290e (12e13b12-be20-4613-bd5a-c0ff0d63617b) f7}
2021-05-28 19:26:18.006 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{1e7290e (12e13b12-be20-4613-bd5a-c0ff0d63617b) f7}
2021-05-28 19:26:18.006 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{1e7290e (12e13b12-be20-4613-bd5a-c0ff0d63617b) f7}
2021-05-28 19:26:18.009 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{1e7290e (12e13b12-be20-4613-bd5a-c0ff0d63617b) f7}
2021-05-28 19:26:18.010 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{1e7290e (12e13b12-be20-4613-bd5a-c0ff0d63617b) f7}
2021-05-28 19:26:18.012 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{bc34be6 (78a5fe4b-4700-4b64-974b-9df2738d7125) f8}
2021-05-28 19:26:18.013 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{bc34be6 (78a5fe4b-4700-4b64-974b-9df2738d7125) f8}
2021-05-28 19:26:18.013 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{bc34be6 (78a5fe4b-4700-4b64-974b-9df2738d7125) f8}
2021-05-28 19:26:18.017 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{bc34be6 (78a5fe4b-4700-4b64-974b-9df2738d7125) f8}
2021-05-28 19:26:18.018 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{bc34be6 (78a5fe4b-4700-4b64-974b-9df2738d7125) f8}
2021-05-28 19:26:18.021 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{ff5f1be (7552f34f-3194-47ce-8abd-f1851309ae2a) f3}
2021-05-28 19:26:18.021 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{ff5f1be (7552f34f-3194-47ce-8abd-f1851309ae2a) f3}
2021-05-28 19:26:18.021 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{ff5f1be (7552f34f-3194-47ce-8abd-f1851309ae2a) f3}
2021-05-28 19:26:18.025 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{ff5f1be (7552f34f-3194-47ce-8abd-f1851309ae2a) f3}
2021-05-28 19:26:18.025 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{ff5f1be (7552f34f-3194-47ce-8abd-f1851309ae2a) f3}
2021-05-28 19:26:18.028 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{c6c7a96 (a923b6ac-2852-4f53-825a-11999d74774b) f2}
2021-05-28 19:26:18.028 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{c6c7a96 (a923b6ac-2852-4f53-825a-11999d74774b) f2}
2021-05-28 19:26:18.029 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{c6c7a96 (a923b6ac-2852-4f53-825a-11999d74774b) f2}
2021-05-28 19:26:18.032 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{c6c7a96 (a923b6ac-2852-4f53-825a-11999d74774b) f2}
2021-05-28 19:26:18.033 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{c6c7a96 (a923b6ac-2852-4f53-825a-11999d74774b) f2}
2021-05-28 19:26:18.036 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{360466e (db4a4dd3-55bc-4096-86c2-ac4c06bc8945) f1}
2021-05-28 19:26:18.036 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{360466e (db4a4dd3-55bc-4096-86c2-ac4c06bc8945) f1}
2021-05-28 19:26:18.036 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{360466e (db4a4dd3-55bc-4096-86c2-ac4c06bc8945) f1}
2021-05-28 19:26:18.040 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{360466e (db4a4dd3-55bc-4096-86c2-ac4c06bc8945) f1}
2021-05-28 19:26:18.041 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{360466e (db4a4dd3-55bc-4096-86c2-ac4c06bc8945) f1}
2021-05-28 19:26:18.043 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onAttach frag=ViewPagerFragment{8d6b546 (0a3a3ad5-8b35-469b-84b6-694d0ceca654) f0}
2021-05-28 19:26:18.044 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreate frag=ViewPagerFragment{8d6b546 (0a3a3ad5-8b35-469b-84b6-694d0ceca654) f0}
2021-05-28 19:26:18.044 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onCreateView frag=ViewPagerFragment{8d6b546 (0a3a3ad5-8b35-469b-84b6-694d0ceca654) f0}
2021-05-28 19:26:18.047 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onActivityCreated frag=ViewPagerFragment{8d6b546 (0a3a3ad5-8b35-469b-84b6-694d0ceca654) f0}
2021-05-28 19:26:18.048 21170-21170/zdq.cn.apemans.multilivedemo D/ViewPagerFragment: onStart frag=ViewPagerFragment{8d6b546 (0a3a3ad5-8b35-469b-84b6-694d0ceca654) f0}

哈哈真有意思,是从f4开始创建到f8 然后又往后创建到f0。
目前就涉及的业务我大概就测试了这些,源码目前还没深入研究,后面有更好的理解再来加深理解,希望大家赶快用上ViewPager2,别再使用ViewPager了,毕竟一不小心使用ViewPager就造成内存泄漏了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容