Align the child views in center of the ViewPager android

前端 未结 7 1045
Happy的楠姐
Happy的楠姐 2020-12-09 17:11

I need to set the child view as center of the ViewPager and also I would like to show some part of the next and previous views to the current view sides(like current screen

相关标签:
7条回答
  • 2020-12-09 17:53

    I found solution in this post, below the code i used:

    // Offset between sibling pages in dp
    int pageOffset = 20;
    
    // Visible part of sibling pages at the edges in dp
    int sidePageVisibleWidth = 10;
    
    // Horizontal padding will be
    int horPadding = pageOffset + sidePageVisibleWidth;
    
    // Apply parameters
    viewPager.setClipToPadding(false);
    viewPager.setPageMargin(UIUtil.dpToPx(pageOffset, getContext()));
    viewPager.setPadding(UIUtil.dpToPx(horPadding, getContext()), 0, UIUtil.dpToPx(horPadding, getContext()), 0);
    

    dpToPx code:

     public static int dpToPx(int dp, Context context) {
            float density = context.getResources().getDisplayMetrics().density;
            return Math.round((float) dp * density);
        }
    

    This is all you need

    0 讨论(0)
提交回复
热议问题