In-depth analysis of the technical principles of the Android Support Library View Page framework in the Java class library VA Class Libraries)
Android Support Library is a powerful tool for developing applications compatible with different Android versions.The ViewPager framework is an important component in the library. It provides developers with a simple and powerful way to create a slippery interface.
This article will in -depth analysis of the technical principles of Android Support Library View Pager framework, and provide some Java code examples to illustrate its working method.
1 Overview
The View Page framework is a container for creating a sliding interface in the application.It allows users to browse different pages by sliding left and right.Usually, each page is an independent Fragment or View.
2. Use ViewPager
To use ViewPager in the application, first of all, you need to introduce Android Support Library dependencies and add ViewPager elements to the layout file.You can then create a Pageradapter that will provide the content of ViewPager.
In Pageradapter, you need to rewrite a few key methods:
-getCount (): Return the total number of pages in ViewPager.
-GetItem (Position: int): Return the corresponding page according to a given position.
-TANTIATEITEM (Container: ViewGroup, Position: int): instantiated and returned to the page object of the specified location.
-Destroyitem (Container: ViewGroup, Position: Int, Object: Any): Destroy the page object of the specified position.
Example code:
public class MyPagerAdapter extends PagerAdapter {
// Number of pages
private static final int NUM_PAGES = 3;
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Exemplate the page according to the location
View view = LayoutInflater.from(container.getContext()).inflate(R.layout.page_layout, container, false);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Destroy the page of the specified location
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
// Determine whether the view comes from an object
return view == object;
}
}
3. Add page surface switching effect
The ViewPager framework also supports adding transition effects between pages.It can be derived by defining the transition effect by achieving the PageRTRANSFORMER interface.For example, you can use ZoomoutPageTransFormer class to add zooming and reduced effect when switching on the page:
Example code:
public class ZoomOutPageTransformer implements ViewPager.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 (pose <-1) {// page has slipped out of the left
page.setAlpha(0f);
} else if (position <= 1) {
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float verticalMargin = pageHeight * (1 - scaleFactor) / 2;
float horizontalMargin = pageWidth * (1 - scaleFactor) / 2;
if (pose <0) {// page is sliding left
page.setTranslationX(horizontalMargin - verticalMargin / 2);
} Else {// Page is sliding to the right
page.setTranslationX(-horizontalMargin + verticalMargin / 2);
}
page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);
page.setAlpha(MIN_ALPHA +
(scaleFactor - MIN_SCALE) /
(1 - MIN_SCALE) * (1 - MIN_ALPHA));
} Else {// Page has slipped out of the right
page.setAlpha(0f);
}
}
}
4. Bind ViewPager and Pageradapter
Finally, you need to institimate ViewPager in Activity or Fragment and bind the Pageradapter with it.
Example code:
ViewPager viewPager = findViewById(R.id.viewPager);
MyPagerAdapter pagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(pagerAdapter);
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
Through the above steps, you can successfully use Android Support Library View Pager framework to create a sliding interface.
Summarize:
This article analyzes the technical principles of Android Support Library View Page framework and provides related Java code examples.The ViewPager framework provides a simple and powerful way to create a sliding interface, and also supports custom transition effects.It is hoped that readers can better understand the working principles and usage methods of the ViewPager framework in Android Support Library.