I’m designing a view with multiple pages. I want edges of previous and next pages to be show like below and implement a 2 finger swipe to switch between pages.
I tried using
ViewPager with negative page margin as suggested here but that only shows one of the edges on the screen, not both simultaneously.
Alternatively, is there any way i can position part of my view outside screen and then animate it giving it a
ViewPager type effect.
How should I go about it ? Thanks !
Quoting myself from a blog post on this subject:
The third approach comes from Dave Smith, co-author of the well-regarded book Android Recipes. He went in a very different direction, using a custom container that disabled children clipping to show more than one page at a time.
His published sample code shows the whole thing in action. His container (
com.example.pagercontainer.PagerContainer) wraps the
setClipChildren(false);on itself, so even though the
ViewPageris focused on one selected page, other pages that have coordinates beyond the
ViewPagerbounds are still visible, so long as they fit within the
PagerContainer. By sizing the
ViewPagerto be smaller than the
ViewPagercan size its pages to that size, leaving room for other pages to be seen.
PagerContainer, though, needs to help out a bit with touch events, as
ViewPagerwill only handle swipe events on its own visible bounds, ignoring any pages visible to the sides.
I have a similar solution:
On the viewpager set left and right padding, e.g. 20dp. Do also set the page margin on the viewpager, e.g. half of the pager padding. And do not forget to disable clip padding.
tilePager.setPadding(defaultGap, 0, defaultGap, 0); tilePager.setClipToPadding(false); tilePager.setPageMargin(halfGap);
Set left and right padding for whole item view. Example xml (page_item.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="20dp" android:paddingRight="20dp"/> <TextView android:id="@+id/text1" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
Then set negative page margin for
PageViewequal to 2*(previous view padding)
int margin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20*2, getResources().getDisplayMetrics()); mViewPager.setPageMargin(-margin);
Optional. Set zero left padding for first item and zero right padding to last item to hide empty edges. You may do this in the
To show preview of left and right pages set the following two values
If you need space between two pages in the viewpager then add viewpager.setPageMargin(int)
if someone still looking for solution, I had customized the ViewPage to achieve it without using negative margin, find a sample project here https://github.com/44kksharma/Android-ViewPager-Carousel-UI
it should work in most cases but you can still define page margin with
mPager.setPageMargin(margin in pixel);
Download the source code from here(ViewPager with previous and next page boundaries)
Sometime ago I needed such feature and prepared a tiny library which uses
RecyclerView with PagerSnapHelper (added in version 25.1.0 of the v7 support library) instead of classic
MetalRecyclerPagerView – you can find all the code along with examples there.
Hope it helps somebody 🙂