片段中滚动视图内的页面视图 Android

Page View inside Scroll View in Fragment Android

我有一个 android 片段布局,我想将我的页面视图放在滚动视图中。当应用程序 运行 没有滚动视图时它显示的图像但是当我将页面视图代码放在滚动视图中时它没有显示图像。

这是我的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.wk.sigah.FragmentHome"
        android:background="@color/colorPrimary">


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v4.view.ViewPager>

        </ScrollView>

</LinearLayout>

Override onMeasure in class and extends ViewPager as follows, this will make it get the height of the biggest child it currently has.

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

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) {

    super.onMeasure(widthMeasureSpec, 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);
}
}

在XML中:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:fillViewport="true">

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

    <com.your.package.name.CustomViewPager
        android:id="@+id/viewImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />

</LinearLayout>

</android.support.v4.widget.NestedScrollView>