Research and Discussion on the technical principle of ViewPager2 framework in Java Library

ViewPager2 is a new type of view switching control in the AndroidX library that is used to achieve multiple page switching effects in Android applications.Compared to the previous ViewPager, ViewPager2 provides a simpler and more flexible API to support more use cases. ViewPager2's framework technical principle is based on RecyclerView.It uses RecyclerView as a layout container, and manages the data of the page through the RecyclerView.adapter, and the switching effect of the page is defined using the ViewPager2.pageTransFormer interface.The following will conduct detailed research and discussion of it. 1. Use of viewpager2 First, add the following dependencies to the project's Build. Gradle file: dependencies { implementation 'androidx.viewpager2:viewpager2:1.0.0-beta04' } Then add ViewPager2 to the layout file: <androidx.viewpager2.widget.ViewPager2 android:id="@+id/viewPager2" android:layout_width="match_parent" android:layout_height="match_parent" /> Next, get the ViewPager2 instance in the code, and set the page switching effect: ViewPager2 viewPager2 = findViewById(R.id.viewPager2); viewPager2.setAdapter(adapter); viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL); viewPager2.setPageTransformer(new ZoomOutPageTransformer()); The adapter here is a custom adapter inherited from the recyclerView.adapter to manage the data of the page.ZoomoutpageTransFormer is a custom page switching effect that needs to achieve viewPager2.pageTransFormer interface. 2. Analysis of the principle of viewpager2 1. Layout container: ViewPager2 uses RecyclerView as the layout container, and uses RecyclerView to implement the sliding and reuse of the page.After the layout is declared ViewPager2, the RecyclerView object will be automatically generated in the code. 2. Page management: ViewPager2 uses RecyclerView.adapter to manage the data of the page.Adapter inherits from RecyclerView.adapter, and rewritten some methods to achieve specific page management logic.For example, rewrite the onCreateViewholder () method to create a page view, and rewrite the onBindViewholder () method to bind the page data. 3. Page switching effect: ViewPager2 defines the switching effect of the page via ViewPager2.pageTransFormer interface.PageTransFormer is an interface, which only contains one transformpage () method, which is used to realize the custom operation of page switching effects according to the location of the page and sliding offset. 4. Event processing: ViewPager2 inherits from Framelayout, which can monitor and handle the user's sliding event.It uses GESTUREDECTOR inside the Touch event to perform more advanced processing.When the user slides, ViewPager2 will switch the page according to the direction of sliding. Third, sample code The following is a simple example code, which is used to demonstrate the basic use and realization of custom page surface switching effects of ViewPager2: public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { private List<String> mData; public MyAdapter(List<String> data) { mData = data; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.bindData(mData.get(position)); } @Override public int getItemCount() { return mData.size(); } } public class MyViewHolder extends RecyclerView.ViewHolder { private TextView mTextView; public MyViewHolder(View itemView) { super(itemView); mTextView = itemView.findViewById(R.id.text_view); } public void bindData(String data) { mTextView.setText(data); } } public class ZoomOutPageTransformer implements ViewPager2.PageTransformer { private static final float MIN_SCALE = 0.85f; private static final float MIN_ALPHA = 0.5f; @Override public void transformPage(@NonNull View page, float position) { int pageWidth = page.getWidth(); int pageHeight = page.getHeight(); if (position < -1 || position > 1) { page.setAlpha(MIN_ALPHA); page.setScaleX(MIN_SCALE); page.setScaleY(MIN_SCALE); } else if (position <= 1) { float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); float vertMargin = pageHeight * (1 - scaleFactor) / 2; float horzMargin = pageWidth * (1 - scaleFactor) / 2; page.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA)); page.setScaleX(scaleFactor); page.setScaleY(scaleFactor); page.setTranslationX(position < 0 ? (horzMargin - vertMargin / 2) : (-horzMargin + vertMargin / 2)); } } } In the above code, MyAdapter inherits from RecyclerView.adapter and is used to manage the data of the page; MyViewholder inherits from RecyclerView.Viewholder, which is used to bind the view of the page; ZoomoutpageTransforms implements viewPage2. PageTransFormer interface is used to achieve page switching effects. In this way, through the above example code, we can use ViewPager2 to achieve multiple page switching effects and customize different page switching effects. Summarize: This article introduces the research and discussion of the technical principle of ViewPager2 framework.ViewPager2 is a new type of view switching control in the AndroidX library, which is based on RecyclerView.RecyclerView is used as a layout container, RecyclerView.adapter to manage the data of the page, ViewPager2.pageTransFormer interface to define the page surface switching effect, and event processing, to achieve the switching effect of multiple pages.By customizing Adapter and PageTransFormer, different page switching needs can be met.The emergence of ViewPager2 makes page switching easier and flexible, becoming one of the important tools for developing Android applications.