Android support library View Pages: nested view and nested slide

Android support library View Pages: nested view and nested slide In Android development, ViewPage is a very commonly used component to achieve the interface display effect of horizontal sliding.However, sometimes we need to display the nested view in the ViewPager and implement the function of nested sliding.This article will introduce how to use the ViewPager in the Android support library, as well as how to embed the view and implement the nested sliding. First of all, we need to add ViewPager support library dependencies to the project.Open the built.gradle file of the project and add the following code to the DependenCies block: groovy implementation 'androidx.viewpager:viewpager:1.0.0' Next, we need to define ViewPager in the layout file.Add the following code to the XML layout file: <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> Now, we can use ViewPager in the code.Find the reference to ViewPager in the Java class and set the adapter for it. ViewPager viewPager = findViewById(R.id.viewPager); viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); In the above code, we set the ViewPager adapter to MyPageradapter.You can customize the adapter according to your needs and provide the logic of the adapter back to each page. In the case of nested sliding, we need to implement the embedded sliding interface to make the ViewPager properly respond to the sliding event of the nested view.To this end, we need to create a custom ViewPager class and implement the NestedScrollingChild2 interface. import androidx.viewpager.widget.ViewPager; import androidx.core.view.NestedScrollingChild2; import androidx.core.view.NestedScrollingChildHelper; public class NestedViewPager extends ViewPager implements NestedScrollingChild2 { private NestedScrollingChildHelper nestedScrollingChildHelper; public NestedViewPager(Context context) { super(context); init(); } public NestedViewPager(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { nestedScrollingChildHelper = new NestedScrollingChildHelper(this); setNestedScrollingEnabled(true); } @Override public boolean startNestedScroll(int axes, int type) { return nestedScrollingChildHelper.startNestedScroll(axes, type); } @Override public void stopNestedScroll(int type) { nestedScrollingChildHelper.stopNestedScroll(type); } @Override public boolean hasNestedScrollingParent(int type) { return nestedScrollingChildHelper.hasNestedScrollingParent(type); } @Override public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow, int type) { return nestedScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow, type); } @Override public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow, int type) { return nestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow, type); } @Override public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) { return nestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed); } @Override public boolean dispatchNestedPreFling(float velocityX, float velocityY) { return nestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY); } } By creating the above nested ViewPager class, we implemented all methods of the NestedScrollingChild2 interface, and provided the correct sliding event response for the nested view. Now, you can use the new nested ViewPager class in the layout file.Replace the ViewPager in the XML layout file to the NestedViewPager, as shown below: <com.example.myapplication.NestedViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> So far, we have successfully achieved the viewpager of nested views and nested sliding.You can add nested views to the adapter according to actual needs, and use the custom NestedViewPager class to respond to the nested event as needed as needed. I hope that this article can understand how to use the ViewPager in the Android support library to achieve nested views and nested sliding.