private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
/**
* Measure specification mode: The parent has not imposed any constraint
* on the child. It can be whatever size it wants.
*/
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
/**
* Measure specification mode: The parent has determined an exact size
* for the child. The child is going to be given those bounds regardless
* of how big it wants to be.
*/
public static final int EXACTLY = 1 << MODE_SHIFT;
/**
* Measure specification mode: The child can be as large as it wants up
* to the specified size.
*/
public static final int AT_MOST = 2 << MODE_SHIFT;
public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
@MeasureSpecMode int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
public static int getMode(int measureSpec) {
//noinspection ResourceType
return (measureSpec & MODE_MASK);
}
...
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
/**
* Utility to return a default size. Uses the supplied size if the
* MeasureSpec imposed no constraints. Will get larger if allowed
* by the MeasureSpec.
*
* @param size Default size for this view
* @param measureSpec Constraints imposed by the parent
* @return The size this view should be.
*/
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
/**
* This method must be called by {@link #onMeasure(int, int)} to store the
* measured width and measured height. Failing to do so will trigger an
* exception at measurement time.
*
* @param measuredWidth The measured width of this view. May be a complex
* bit mask as defined by {@link #MEASURED_SIZE_MASK} and
* {@link #MEASURED_STATE_TOO_SMALL}.
* @param measuredHeight The measured height of this view. May be a complex
* bit mask as defined by {@link #MEASURED_SIZE_MASK} and
* {@link #MEASURED_STATE_TOO_SMALL}.
*/
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
...
setMeasuredDimensionRaw(measuredWidth, measuredHeight);
}
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.vcredit.vmoney.utils.CommonUtils;
import com.vcredit.vmoney.utils.ConstantUtils;
/**
* Created by qiubangbang on 2016/8/16.
*/
public class WrapContentViewpager extends ViewPager {
// private static final String TAG = "qbb_MyButton";
private int[] pageHeigeht = {0, 0, 0};
public WrapContentViewpager(Context context) {
super(context);
}
public WrapContentViewpager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
CommonUtils.LOG_D(getClass(), "inestmentpages: onMeasure");
CommonUtils.LOG_D(getClass(), "inestmentpages: onMeasure_item: " + getCurrentItem());
CommonUtils.LOG_D(getClass(), "inestmentpages: getchildCount: " + getChildCount());
// 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;
// }
// }
View child;
if (getChildCount() > 0) {
//测量展示的viewpager页的高度
//只有pageheight没有值时采取测量
if (pageHeigeht[0] == 0 || pageHeigeht[1] == 0 || pageHeigeht[2] == 0) {
//点击时出现问题,如果不是按部就班滑动,而是直接点击第三页
if (pageHeigeht[1] == 0 && getCurrentItem() == 2) {
child = getChildAt(1);
} else if (pageHeigeht[2] != 0 && getCurrentItem() == 1) {
child = getChildAt(0);
} else {
if (getChildCount() == 2 && getCurrentItem() == 2) {
child = getChildAt(1);
} else {
child = getChildAt(getCurrentItem());
}
}
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
if (pageHeigeht[getCurrentItem()] == 0) {
pageHeigeht[getCurrentItem()] = child.getMeasuredHeight();
}
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(pageHeigeht[getCurrentItem()], MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}