Viewpager with Previous and Next Page Boundaries

ViewPager with previous and next page boundaries

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 ViewPager and calls setClipChildren(false); on itself, so even though the ViewPager is focused on one selected page, other pages that have coordinates beyond the ViewPager bounds are still visible, so long as they fit within the PagerContainer. By sizing the ViewPager to be smaller than the PagerContainer, the ViewPager can 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 ViewPager will only handle swipe events on its own visible bounds, ignoring any pages visible to the sides.

Sample Image

How can i create ViewPager with previous and next page preview, and centred one zoomed from preview


viewPagerMusicCategory.setPageTransformer(false, new ViewPager.PageTransformer() {
@Override
public void transformPage(View page, float position) {
Log.e("pos",new Gson().toJson(position));
if (position < -1) {
page.setScaleY(0.7f);
page.setAlpha(1);
} else if (position <= 1) {
float scaleFactor = Math.max(0.7f, 1 - Math.abs(position - 0.14285715f));
page.setScaleX(scaleFactor);
Log.e("scale",new Gson().toJson(scaleFactor));
page.setScaleY(scaleFactor);
page.setAlpha(scaleFactor);
} else {
page.setScaleY(0.7f);
page.setAlpha(1);
}
}
}
);

....

Android ViewPager with previous and next pages visible?

I used a negative page margin to partly show the next and the previous pages. The fading edge property can be used to make previous/next page fade:

ViewPager examplePager = (ViewPager) findViewById(R.id.exampleView);
examplePager.setPageMargin(-50);
examplePager.setHorizontalFadingEdgeEnabled(true);
examplePager.setFadingEdgeLength(30);

The lastest support package (revision 4, October 2011) is also required for this to work

RecyclerView with previous and next page boundaries?


But how to accomplish this with RecyclerViews? The items have to be
equally spread, and the current item should be centered.

You can just set the width of ViewHolder item view to achieving this effect in RecyclerView.

And you should use LinearSnapHelper instead of PagerSnapHelper.

why setCurrentItem() of viewPager works for next page and not for previous page?

Refering to the Tabbed Activity, take a look at the section number as the fragment is initialized. (sectionNumber = position+1).

    @Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}

Therefore, ARG_SECTION_NUMBER-1 refers to the position of the current fragment, ARG_SECTION_NUMBER-2 to the previous, and ARG_SECTION_NUMBER to the next.

Consequently, your code should be like this:

 nextAbitudini.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//it works
mViewPager.setCurrentItem(getArguments().getInt(ARG_SECTION_NUMBER));
}
});

backAbitudini.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//it doesn't work
mViewPager.setCurrentItem(getArguments().getInt(ARG_SECTION_NUMBER)-2);
}
});


Related Topics



Leave a reply



Submit