android - ObservableScrollView 中的自定义 ViewPager 无法正确测量高度

标签 android android-fragments android-viewpager scrollview

我在 ObservableScrollView 中有一个 CustomViewPager,如下所示:

enter image description here

它似乎测量 fragment 但不测量屏幕外 fragment 的高度。所以我实际上无法向上滚动。

这是 CustomViewPager 的代码:

public class CustomViewPager extends ViewPager {

private View view;

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

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

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;

    View tab = getChildAt(getCurrentItem());
    int width = getMeasuredWidth();
    int tabHeight = tab.getMeasuredHeight();

    if (wrapHeight) {
        // Keep the current measured width.
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
    }

    int fragmentHeight = measureFragment(((Fragment) getAdapter().instantiateItem(this, getCurrentItem())).getView());
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(tabHeight + fragmentHeight + (int)
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()), MeasureSpec.AT_MOST);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

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

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

}

注意:如果我在 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 中将 + 1000 添加到 heightMeasureSpec > 然后我可以滚动 fragment 的大小以及任何额外的空间。但显然这不是首选解决方案。

这是我的 XML 文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
    android:id="@+id/infoBox"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="240dp">

    <!-- Code for other views -->

</LinearLayout>

<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none">

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

        <com.ui.customviewpager.CustomViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </LinearLayout>

</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>

这似乎是什么问题?看来我不是唯一遇到此问题的人。

我从这个链接 here 实现了一些设计

最佳答案

ScrollView child 不应该有 margin 来让 scrollView 正确显示。 看起来当 scrollView 测量它的 child 时,边距没有被考虑在内。

尝试移除 LinearLayout 的边距(您可以使用填充来重现效果)。

关于android - ObservableScrollView 中的自定义 ViewPager 无法正确测量高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29058385/

相关文章:

android - 从 fragment 到 Activity 问题的接口(interface)通信

android - 如何在滑动页面时播放短声音?

android - ViewPager 以编程方式滚动

android - 将数据从 Activity 传递到 ViewPager 中的 Fragment

java - 在 Android 中使用 MySQL 和 PHP

android - Ionic android 请求位置许可

java - 如何使用 BaseAdapter 将子 fragment 添加到 Android 中的 fragment

android - 如何增加android应用程序的堆大小?

php - Android 和 HTML 页面之间的 Webview 数据不匹配

android - 将监听器重置为父 Activity 以重新创建 fragment