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 theViewPager
and callssetClipChildren(false);
on itself, so even though theViewPager
is focused on one selected page, other pages that have coordinates beyond theViewPager
bounds are still visible, so long as they fit within thePagerContainer
. By sizing theViewPager
to be smaller than thePagerContainer
, theViewPager
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, asViewPager
will only handle swipe events on its own visible bounds, ignoring any pages visible to the sides.
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
Bitmapfactory Oom Driving Me Nuts
Finish an Activity from Another Activity
Error Opening Supportmapfragment for Second Time
How to Disable Action Bar Permanently
How to Get the Touch Position in Android
Android: Out of Memory Exception in Gallery
Dynamically Changing the Fragments Inside a Fragment Tab Host
Pass a String from One Activity to Another Activity in Android
How to Kill an Application with All Its Activities
Passing Arraylist of Objects Between Activities
Get the Current Language in Device
Datepicker: How to Popup Datepicker When Click on Edittext
How to Prevent Multiple Instances of an Activity When It Is Launched with Different Intents
How to Hide a Menu Item in the Actionbar
Google Sign in Not Working After Publishing in Play Store