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

时间:2018-03-26 10:24:17

标签: java android fragment scrollview

我有一个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>

1 个答案:

答案 0 :(得分:3)

  

在课程中覆盖onMeasure并扩展ViewPager,如下所示   将使它达到目前最大的孩子的身高。

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>

Tutorial