如果设置layout_height = wrap_content,则ViewPager不工作

使用ViewPaper时遇到问题。 它不显示任何东西,当我设置ViewPaper的height = 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://stackoverflow.com/a/24666987/1928560

Solutions Collecting From Web of "如果设置layout_height = wrap_content,则ViewPager不工作"

ViewPager高度必须指定match_parent否则您必须为其指定一个值。 两种解决scheme

  1. LinearLayout使用权重概念
  2. 在类文件中使用onMeasure方法。 请参阅Android:我无法使用ViewPager WRAP_CONTENT

这是你如何在你的LinearLayout使用权重

  <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共享相同的高度。 您可以通过更改重量值来更改比率

惠在那里,我也使用这个,请尝试一下,看看它是否能解决你的问题。

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

当viewpager height设置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; } } 

我们可以自定义ViewPager来将ViewPager的大小调整为当前的页面大小。 你可以使用下面的代码。

  public class WrapContentViewPager extends ViewPager { private int mCurrentPagePosition = 0; public WrapContentViewPager(Context context) { super(context); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { View child = getChildAt(mCurrentPagePosition); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } } catch (Exception e) { e.printStackTrace(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void reMeasureCurrentPage(int position) { mCurrentPagePosition = position; requestLayout(); } } 

在XML而不是使用android.support.v4.view.ViewPager使用WrapContentViewPager。

 <WrapContentViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="wrap_content"> </WrapContentViewPager> 

调用页面上的reMeasureCurrentpage函数。

  final WrapContentViewPager wrapContentViewPager = (WrapContentViewPager) findViewById(R.id.view_pager); wrapContentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { wrapContentViewPager.reMeasureCurrentPage(wrapContentViewPager.getCurrentItem()); } @Override public void onPageScrollStateChanged(int state) { } });