android 开发中ScrollView 嵌套 ViewPager 不能显示解决办法

在ScrollView 中嵌套ViewPager 如果ViewPager高度使用wrap_content
就会出现无法显示问题

解决办法:
1.给ViewPager 设定固定高度(不推荐)
2.自定义ViewPager 重写ViewPager测量方法

<com.guanyueyun.mylook.view.CustomViewPager
            android:id="@+id/view_home_main_viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white" />
public class CustomViewPager extends ViewPager {

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height)height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

你可能感兴趣的:(android-开发)