如何使ScrollView中的ViewPager高度为MATCH_PARENT?

时间:2017-08-19 02:34:09

标签: android android-viewpager android-scrollview

以前我在FragmentViewPager显示ScrollView时出现问题。问题是因为我们需要为ViewPager设置确切的高度。我们不能依赖WRAP_CONTENTMATCH_PARENT。我用here的代码解决了这个问题。现在,我的问题是如果Fragment的高度不是全屏,我无法使用屏幕的空白部分滑动ViewPager。我只能使用Fragment的可见部分进行滑动。以下是代码。

public class CustomPager extends ViewPager {

    private View mCurrentView;

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mCurrentView == null) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        int height = 0;
        mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = mCurrentView.getMeasuredHeight();
        if (h > height) height = h;

        //TODO: if (height < empty space height of a screen) height = empty space height of a screen

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

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void measureCurrentView(View currentView) {
        mCurrentView = currentView;
        requestLayout();
    }

    public int measureFragment(View view) {
        if (view == null)
            return 0;

        view.measure(0, 0);
        return view.getMeasuredHeight();
    }
}

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <com.example.CustomPager
            android:id="@+id/custom_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:1)

尝试这种方式适用于我的情况  男性你的com.example.CustomPager

 <com.example.CustomPager
        android:id="@+id/custom_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

并在您的片段中将NestedScrollView作为父级布局

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context="com.example.Fragment">

并制作ScrollView android:fillViewport="true"

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"
   android:fillViewport="true"
   android:layout_height="match_parent">

答案 1 :(得分:1)

尝试此解决方案。

<强>机器人:fillViewport = “真”

data: