模拟b站首页出现NestedScrollView嵌套ViewPager出现页面不显示问题:
结局方案:重写ViewPager的onMeasure方法
/**
* author: wmr
* date: 2016/12/02
* version: 1.0
* description:自适应子View高度的viewPager:ViewPager的onMeasure方法
*/
public classAutoHeightViewPagerextendsViewPager {
publicAutoHeightViewPager(Context context,AttributeSet attrs) {
super(context,attrs);
}
@Override
protected voidonMeasure(intwidthMeasureSpec, intheightMeasureSpec) {
intheight =0;
// 下面遍历所有child的高度
for(inti =0;i < getChildCount();i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec,
MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
inth = child.getMeasuredHeight();
// 采用最大的view的高度
if(h > height) {
height = h;
}
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}
}