ViewPager: set a different pad for the first and last page

I have already implemented the "page view" function for my ViewPager:

mPager.setClipToPadding(false); mPager.setPadding(120, 0, 120, 0); mPager.setPageMargin(60); 

By doing this, I can view part of the previous and next page. But the first and last page show more blank space because there is no other page in that direction.

How can I install another add-on for the first and last page?

+5
source share
1 answer

I had to solve the same problem, and I solved it by installing a custom PageTransformer . I practically drag pages when in first and last place. Let me know if this works for you.

 mPager.setClipToPadding(false); mPager.setPadding(120, 0, 120, 0); mPager.setPageMargin(60); mPager.setPageTransformer(false, new ViewPager.PageTransformer() { @Override public void transformPage(View page, float position) { if (mPager.getCurrentItem() == 0) { page.setTranslationX(-120); } else if (mPager.getCurrentItem() == adapter.getCount() - 1) { page.setTranslationX(120); } else { page.setTranslationX(0); } } }); 
+7
source

Source: https://habr.com/ru/post/1259683/


All Articles