布局正常写,代码添加以下部分即可
private void setViewPagerEffect(int leftItemWidth, int rightItemWidth, int pageMargin) {
CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
compositePageTransformer.addTransformer(new MarginPageTransformer(ViewUtil.dp2px(pageMargin)));
binding.cardViewPager.setPageTransformer(compositePageTransformer);
setRecyclerViewPadding(ViewUtil.dp2px(leftItemWidth + pageMargin), ViewUtil.dp2px(rightItemWidth + pageMargin));
}
private void setRecyclerViewPadding(int leftItemPadding, int rightItemPadding) {
RecyclerView recyclerView = (RecyclerView) binding.cardViewPager.getChildAt(0);
if (binding.cardViewPager.getOrientation() == ViewPager2.ORIENTATION_VERTICAL) {
recyclerView.setPadding(0, leftItemPadding, 0, rightItemPadding);
} else {
recyclerView.setPadding(leftItemPadding, 0, rightItemPadding, 0);
}
recyclerView.setClipToPadding(false);
}
网友评论