您可以自定义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中声明它:
在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) { } });
我真的很喜欢@ abhishek-v的答案,但做了一些升级:
addPageChangeListener(...)
在我们内部ViewPager
调用(不需要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); } }
终于找到了更好的WrapContentViewPager解决方案!
另一个解决方案包含一些难以解决的错误,但我认为这个解决方案可以简单地应用在我们的代码中
我在这个网站上找到了它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); } }