继承一个viewpager,建立一个方法
public int getChildWidth(int p)
{
View child = getChildAt(p);
if (child!=null)
{
int h = child.getMeasuredWidth();
return h;
}
else {
return 0;
}
}
vp中设置
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//计算ViewPager现在应该的高度,heights[]表示页面高度的数组。
int width = (int) (vp.getChildWidth(position));
// Log.e("TAG","width"+width);
//为ViewPager设置高度
if (width!=0){
ViewGroup.LayoutParams params =vp.getLayoutParams();
params.width = width;
vp.setLayoutParams(params);
}
}
@Override
public void onPageSelected(int position) {}
@Override
public void onPageScrollStateChanged(int state) {}
});
网友评论