我在 ConstraintLayout 下添加了一个 viewpager2 并使用 PageTransformer 设置以显示上一页和下一页预览
private void setPageMargin(ViewPager2 viewpager2) {
viewpager2.setOffscreenPageLimit(3);
float nextItemVisiblePx = mContext.getResources().getDimension(R.dimen.viewpager_next_item_visible);
float currentItemHorizontalMarginPx = mContext.getResources().getDimension(R.dimen.viewpager_current_item_horizontal_margin);
viewpager2.setPageTransformer((page, position) -> {
float offset = position * -(2 * nextItemVisiblePx + currentItemHorizontalMarginPx);
if (ViewCompat.getLayoutDirection(viewpager2) == ViewCompat.LAYOUT_DIRECTION_RTL) {
page.setTranslationX(-offset);
} else {
page.setTranslationX(offset);
}
if (position < -1)
page.setAlpha(0.5f);
else if (position <= 1) {
page.setAlpha(Math.max(0.5f, 1 - Math.abs(position)));
} else page.setAlpha(0.5f);
});
}
当我设置 viewpager2.setcurrentItem(pos) 时,它会随机选择错误的页面。请任何人都可以指出这里可能出现的问题?