ViewPager2 + Fragment两边各展示一点点Fra
2021-08-03 本文已影响0人
清朝公主大清正黄旗
布局正常写,代码添加以下部分即可
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);
}