如果将layout_height设置为wrap_content,则ViewPager无法工作

13

我在使用 ViewPaper 时遇到了问题。当我将 ViewPaper 的高度设置为 wrap_content 时,它不显示任何内容;但是如果我为 ViewPager 设置高度 (例如:android:layout_height= "350dp"),则可以正常显示。请帮忙解决!谢谢!

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <ScrollView
        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:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="#FF0000"
                android:gravity="center"
                android:text="Quick Links"
                android:textColor="@color/white"
                android:textSize="20sp" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

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

                <com.viewpagerindicator.CirclePageIndicator
                    android:id="@+id/circlePage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/pager"
                    android:layout_marginBottom="10dp" >
                </com.viewpagerindicator.CirclePageIndicator>
            </RelativeLayout>

            <com.custom.ExpandableHeightListView
                android:id="@+id/lvAdmissions"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:divider="@drawable/bg_listview_divider"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/selector_animation" >
            </com.custom.ExpandableHeightListView>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

已解决: 我跟随这个链接并且它帮助我解决了这个问题。 https://dev59.com/K2oy5IYBdhLWcg3wq_7O#24666987

3个回答

12

ViewPager的高度必须要么指定为match_parent,要么您必须为其指定一个值。有两种解决方案:

  1. LinearLayout中使用权重(weights)的概念
  2. 在您的类文件中使用onMeasure方法。请参见Android: I am unable to have ViewPager WRAP_CONTENT

以下是如何在您的LinearLayout中使用权重(weights)的方法:

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#FF0000"
            android:gravity="center"
            android:text="Quick Links"
            android:textColor="@color/white"
            android:textSize="20sp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

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

            <com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/circlePage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/pager"
                android:layout_marginBottom="10dp" >
            </com.viewpagerindicator.CirclePageIndicator>
        </RelativeLayout>

        <com.custom.ExpandableHeightListView
            android:id="@+id/lvAdmissions"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:divider="@drawable/bg_listview_divider"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/selector_animation" >
        </com.custom.ExpandableHeightListView>
    </LinearLayout>

这样您的viewpager布局和ExpandableListView高度相等。您可以通过更改权重值来改变比例。


10

=> 当设置viewpager高度为wrap_content时,100%有效。

public class FollowUseWrapHeightViewPager extends ViewPager {
public FollowUseWrapHeightViewPager(Context context) {
    super(context);
}

public FollowUseWrapHeightViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = 0;
    View view = null;
    for(int i = 0; i < getChildCount(); i++) {
        view = getChildAt(i);
        view.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = view.getMeasuredHeight();
        if(h > height) height = h;
    }

    if (height != 0) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
}

/**
 * Determines the height of this view
 *
 * @param measureSpec A measureSpec packed into an int
 * @param view the base view with already measured height
 *
 * @return The height of the view, honoring constraints from measureSpec
 */
private int measureHeight(int measureSpec, View view) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        result = specSize;
    } else {
        // set the height from the base view if available
        if (view != null) {
            result = view.getMeasuredHeight();
        }
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return result;
}
}

1
逐一获取所有子元素的高度,并将其高度设置在ViewPager中。 - Hitesh Tarbundiya
没错,它可以工作。 实际上,它可以让你朝着正确的方向开始,谢谢! - Jivraj S Shekhawat

2
我也在使用这个,试试看它是否能解决你的问题。
<LinearLayout
    android:id="@+id/container_app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

   <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

1
它没有起作用。我希望它应该包括页面指示器。无论如何,感谢您的评论。 - Phong NGUYEN

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