ViewPager wrap_content无法正常工作

我做了一个滚动视图,里面有一个viewpager。 但是viewpager的高度并没有增长。 Wen在viewpager里面的内容是大到内部得到pucht,表是收缩的空间。

我已经搜索了一个解决方案,发现问题是“wrap_content”,如果viewpager被加载则没有内容。 所以viewpager保持屏幕高度。 我find的解决方案不适用于“FragmentPagerAdapter”,所以我编辑了这个问题。

请指出我正确的方向。

Greathings christophe VD

主要布局是这样,底部的时间标记是问题之一:
在此处输入图像描述
它需要更多这样的:
在此处输入图像描述

FragmentPagerAdapter是:

import android.content.Context; import android.graphics.pdf.PdfDocument; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class film_tabs_adapter extends FragmentPagerAdapter { private final String[] TITLES = { "Info", "Comment", "Cast", "Muziek"}; String[] Film_Data; public film_tabs_adapter(FragmentManager fm, String[] Film_Data_in) { super(fm); Film_Data = Film_Data_in; } @Override public CharSequence getPageTitle(int position) { return TITLES[position]; } @Override public int getCount() { return 4; } @Override public Fragment getItem(int position) { Fragment fragment =null; Bundle bundle = new Bundle(); bundle.putStringArray("Film_Data", Film_Data); switch(position) { case 0: //return new film_info(); fragment = new film_info(); fragment.setArguments(bundle); return fragment; case 1: return new film_coment(); case 2: return new film_cast(); case 3: return new film_sound(); default: return new film_info(); } } } 

和片段的类:

  import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; public class film_info extends Fragment { TextView label_locatie; ... TableLayout TimeTabel; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_film_info, container, false); // layout (activity_film_info) openen Context context = inflater.getContext(); DatabaseVerwerker DB = new DatabaseVerwerker(context); String[] Film_Data = getArguments().getStringArray("Film_Data"); ... return view; } } 

活动:

                            

碎片

                                             

Related of "ViewPager wrap_content无法正常工作"

您可以自定义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 { boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST; if (wrapHeight) { 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中声明它:

    

之后调用reMeasureCurrentpagefunction在页面上滑动。

  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) { } }); 

我真的很喜欢@ abhishek-v的答案,但做了一些升级:

  • 在我们的ViewPager中调用reMeasureCurrentPage(...)不需要在页面滑动外手动调用reMeasureCurrentPage(...)
  • 不需要存储mCurrentPagePosition ,因为我们可以使用getCurrentItem()
  • 删除废物try/catch

ViewPager现在看起来像:

 public class WrapContentViewPager extends ViewPager { public WrapContentViewPager(Context context) { super(context); initPageChangeListener(); } public WrapContentViewPager(Context context, AttributeSet attrs) { super(context, attrs); initPageChangeListener(); } private void initPageChangeListener() { addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { requestLayout(); } }); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { View child = getChildAt(getCurrentItem()); if (child != null) { child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } 

终于find了更好的WrapContentViewPager解决方案!

另一个解决方案包含一些难以解决的错误,但我认为这个解决方案可以简单地应用在我们的代码中

我在这个网站上find了它https://github.com/akhahaha/burn-android/blob/master/app/src/main/java/com/ucla/burn/android/WrapContentViewPager.java

这是代码

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

在Android 8.0上,您必须调用另一个requestLayout才能使其工作:

 ....@Abhishek's answer sharePager.post(new Runnable() { @Override public void run() { sharePager.requestLayout(); } }); 

如果这些答案仍然无法解决问题,我可以提供另一种选择。 在PagerAdapter java文件中,您可以手动测量page高度,并在instantiateItem方法中设置容器的LayoutParams.height 。 喜欢这个:

 @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { View view = generateView(mContext, position); view.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.getScreenWidth(mContext), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); container.getLayoutParams().height = view.getMeasuredHeight(); container.addView(view); return view; }