Android Market/Google Music seem to be able to have a gap of some sort between the different fragments that are contained in the ViewPager
.
Any idea how this is done? Adding margin/padding to the actual fragment view doesn't work, because the view still needs to occupy the entire width of the screen. The 'gap' is only visible when swiping the ViewPager.
In Support Package, r4 you can use these methods:
setPageMargin(int marginPixels)
setPageMarginDrawable(Drawable)
setPageMarginDrawable(int)
in ViewPager class.
I don't know why this doesn't have equivalent in XML :-(
If you need use dip unit you can use this method:
public static int convertDip2Pixels(Context context, int dip) {
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());
}
Hoping this might help someone with a ViewPager and page margins.
I wanted to have a ViewPager is a CardView and had issues with the padding and the margin between the viewpager pages. The setPageMargin did not work for me at all. I had to use the following:
int pagerPadding = 16;
pager.setClipToPadding(false);
pager.setPadding(pagerPadding, 0, pagerPadding, 0);
This allow the pages to be very close together.
viewPager.setPageMargin(dpToPx(10));
// replace 10 with the value you want for margin in dp.
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
Try This .
ViewPager viewPager = (ViewPager) findViewById(R.id.h_view_pager);
viewPager.setClipToPadding(false);
viewPager.setPadding(60, 0, 60, 0);
viewPager.setPageMargin(20);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setOffscreenPageLimit(adapter.getCount());
viewPager.setAdapter(adapter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With