模擬b站首頁出現(xiàn)NestedScrollView嵌套ViewPager出現(xiàn)頁面不顯示問題:
結(jié)局方案:重寫ViewPager的onMeasure方法
/**
* author: wmr
* date: 2016/12/02
* version: 1.0
* description:自適應(yīng)子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);
}
}