ViewPager2的正确使用姿势

一、使用方式

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

public class ViewPager2Activity extends AppCompatActivity {

    ViewPager2 viewpager2;
    ViewPager2Adapter viewPager2Adapter;
    List viewPagerFragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager2);
        viewpager2 = findViewById(R.id.viewpager2);
        viewPagerFragments = new ArrayList();
        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 mdatas;

    public ViewPager2Adapter(@NonNull FragmentActivity fragmentActivity, List 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 的话 一定要给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就造成内存泄漏了。

你可能感兴趣的:(ViewPager2的正确使用姿势)