ViewPager2 切换时高度问题
vpRegister.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
View view = fragmentList.get(position).getView();
updatePagerHeightForChild(view, vpRegister);
}
// 解决ViewPager2 切换时高度问题
private void updatePagerHeightForChild(View view, ViewPager2 pager) {
view.post(() -> {
int wMeasureSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int hMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(wMeasureSpec, hMeasureSpec);
if (pager.getLayoutParams().height != view.getMeasuredHeight()) {
ViewGroup.LayoutParams layoutParams = pager.getLayoutParams();
layoutParams.height = view.getMeasuredHeight();
pager.setLayoutParams(layoutParams);
}
});
}
});
|