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

3

之前我遇到了一个问题,即在ScrollView中显示来自ViewPagerFragment时遇到了问题。这是因为我们需要为ViewPager设置准确的高度。我们不能依赖于WRAP_CONTENTMATCH_PARENT。我从这里解决了这个问题。现在,我的问题是如果一个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>

分享你的XML布局。 - AskNilesh
如果您使用 ScrollView 来滚动 ViewPager 的内容,请不要这样做,将 ScrollView 移动到各个片段的布局中。如果您只是因为其他原因而在 ViewPager 外部拥有一个 ScrollView,请在 ScrollView 上设置 fillViewport 属性为 true。 - Gergely Kőrössy
@NileshRathod 完成。 - Sam
@GergelyKőrössy 我之前尝试过使用 android:fillViewport=true,但没有生效。 - Sam
2个回答

2
尝试这个解决方案。 android:fillViewport="true"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:fillViewport="true"
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" />

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


2
严肃地说,我之前尝试过放置 android:fillViewport="true",但它没有起作用。它什么也没改变。 - Sam

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">

它对我不起作用。Fragment被显示出来,但是当我在空白部分滑动时,无法滑动它。 - Sam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接