衡量一个ViewPager [英] Measuring a ViewPager
问题描述
我有一个自定义的ViewGroup,有一个孩子 ViewPager
。该ViewPager由 PagerAdapter
,提供了一个的LinearLayout
到 ViewPager $ C喂$ C>具有
的LayoutParams
的 WRAP_CONTENT
的高度和宽度。
I have a custom ViewGroup that has a child ViewPager
. The ViewPager is fed by a PagerAdapter
that provides a LinearLayout
to the ViewPager
which has LayoutParams
of WRAP_CONTENT
on both height and width.
视图显示正确但当 child.measure()
方法被调用的ViewPager它不返回的LinearLayout的实际尺寸,但似乎填满所有的剩余空间。
The view displays correctly but when the child.measure()
method is called on the ViewPager it does not return the actual dimensions of the LinearLayout but seems to fill all the remaining space.
任何想法,为什么发生这种情况,以及如何修改呢?
Any ideas why this is happening and how to amend it?
推荐答案
我是不是很高兴与接受的答案(也不与pre-膨胀 - 所有的意见解决方案,在评论),所以我把一起 ViewPager
这需要它的高度,从第一个可用的孩子。它通过做第二次测量关,让你偷的第一个孩子的身高做到这一点。
I wasn't very happy with the accepted answer (nor with the pre-inflate-all-views solution in the comments), so I put together a ViewPager
that takes its height from the first available child. It does this by doing a second measurement pass, allowing you to steal the first child's height.
有一个更好的解决办法是使 android.support.v4.view
包,实现了 onMeasure <一个更好的版本里的新类/ code>(可访问包可见的方法,如
填充()
)
A better solution would be to make a new class inside the android.support.v4.view
package that implements a better version of onMeasure
(with access to package-visible methods like populate()
)
有关目前,虽然,下面的解决方案适合我就好了。
For the time being, though, the solution below suits me fine.
public class HeightWrappingViewPager extends ViewPager {
public HeightWrappingViewPager(Context context) {
super(context);
}
public HeightWrappingViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec)
== MeasureSpec.AT_MOST;
if(wrapHeight) {
/**
* The first super.onMeasure call made the pager take up all the
* available height. Since we really wanted to wrap it, we need
* to remeasure it. Luckily, after that call the first child is
* now available. So, we take the height from it.
*/
int width = getMeasuredWidth(), height = getMeasuredHeight();
// Use the previously measured width but simplify the calculations
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
/* If the pager actually has any children, take the first child's
* height and call that our own */
if(getChildCount() > 0) {
View firstChild = getChildAt(0);
/* The child was previously measured with exactly the full height.
* Allow it to wrap this time around. */
firstChild.measure(widthMeasureSpec,
MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
height = firstChild.getMeasuredHeight();
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
这篇关于衡量一个ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!