1. The introduction

ViewPager2 is an updated version of ViewPager. In this article, we will briefly explain how to use ViewPager2, FragmentStateAdapter, and Fragment to slide between pages.

2. Slide to switch between pages

2.1 Introduction of ViewPager2 library

To use ViewPager2, we need to import the ViewPager2 library as follows:

implementation "Androidx. Viewpager2: viewpager2:1.0.0."
Copy the code

2.2 use ViewPager2

Use ViewPager2 in a layout as shown in the following example:

<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/viewPager2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
Copy the code

2.3 build fragments

This Fragment is for simple demonstration only. Its layout is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="@color/black"/>

</LinearLayout>
Copy the code

The ContentFragment class is implemented as follows:

public class ContentFragment extends Fragment {
    private String content;

    public ContentFragment(String content) {
        this.content = content;
    }

    private TextView tv_content;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, container, false);
        tv_content = view.findViewById(R.id.tv_content);
        tv_content.setText(content);
        return view;
    }

    public void setContent(String content) {
        this.content = content; tv_content.setText(content); }}Copy the code

2.4 inheritance FragmentStateAdapter

Create a custom ContentPagerAdapter class that inherits the FragmentStateAdapter and implements the createFragment(int Position) and getItemCount() methods, as shown in the following example:

public class ContentPagerAdapter extends FragmentStateAdapter {
    private List<ContentFragment> datas;

    public ContentPagerAdapter(@NonNull FragmentActivity fragmentActivity,List<ContentFragment> datas) {
        super(fragmentActivity);
        this.datas = datas;
    }

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

    @Override
    public int getItemCount(a) {
        returndatas.size(); }}Copy the code

2.5 Bind ViewPager2 to the adapter

Once you bind ViewPager2 to the adapter, you can slide between pages as shown in the following example:

datas = new ArrayList<>();
datas.add(new ContentFragment("Page 1"));
datas.add(new ContentFragment("Page 2"));
datas.add(new ContentFragment("Page 3"));
datas.add(new ContentFragment("Page 4"));
datas.add(new ContentFragment("Page 5"));
contentPagerAdapter = new ContentPagerAdapter(this, datas);
viewPager2.setAdapter(contentPagerAdapter);
Copy the code

2.6 Vertical sliding switch

ViewPager2 supports both horizontal and vertical sliding, which is fairly easy to achieve by adding the Android :orientation=”vertical” attribute to the layout file, as shown below:

<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/viewPager2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"/>
Copy the code

You can also make ViewPager2 slide vertically by calling the setOrientation(viewPager2.orientation_vertical) method in your code.

2.7 Fragment updates

When the Fragment collection changes and needs to be updated, it is also convenient to use FragmentStateAdapter to update. Because ViewPager2 is implemented based on RecyclerView, NotifyItemChanged (int Position) and notifyItemInserted(int Position) can be used to update data.

3. Summary

Using ViewPager2, FragmentStateAdapter and Fragment can easily achieve sliding between pages. It not only supports horizontal sliding, but also can achieve vertical sliding through simple Settings. Flexible use of ViewPager2 can achieve actual needs.