scrollview里面嵌套了edittext,当输入文字行数过多,超出edittext高度需要滑动才能查看时,发现和scrollview的滑动产生了冲突;
public classMyEditTextextendsEditText {
//滑动距离的最大边界
private intmOffsetHeight;
//是否到顶或者到底的标志
private booleanmBottomFlag=false;
publicMyEditText(Context context) {
super(context);
init();
}
publicMyEditText(Context context,AttributeSet attrs) {
super(context,attrs);
init();
}
publicMyEditText(Context context,AttributeSet attrs, intdefStyleAttr) {
super(context,attrs,defStyleAttr);
init();
}
private voidinit() {
}
@Override
protected voidonMeasure(intwidthMeasureSpec, intheightMeasureSpec) {
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
intpaddingTop;
intpaddingBottom;
intmHeight;
intmLayoutHeight;
//获得内容面板
Layout mLayout = getLayout();
//获得内容面板的高度
mLayoutHeight = mLayout.getHeight();
//获取上内边距
paddingTop = getTotalPaddingTop();
//获取下内边距
paddingBottom = getTotalPaddingBottom();
//获得控件的实际高度
mHeight = getHeight();
//计算滑动距离的边界
mOffsetHeight= mLayoutHeight + paddingTop + paddingBottom - mHeight;
}
@Override
public booleandispatchTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
//如果是新的按下事件,则对mBottomFlag重新初始化
mBottomFlag=false;
//如果已经不要这次事件,则传出取消的信号,这里的作用不大
if(mBottomFlag)
event.setAction(MotionEvent.ACTION_CANCEL);
return super.dispatchTouchEvent(event);
}
@Override
public booleanonTouchEvent(MotionEvent event) {
booleanresult =super.onTouchEvent(event);
//如果是需要拦截,则再拦截,这个方法会在onScrollChanged方法之后再调用一次
if(!mBottomFlag&&this.getLineCount()>6)
// if (!mBottomFlag&&this.hasFocus())
getParent().requestDisallowInterceptTouchEvent(true);
returnresult;
}
@Override
protected voidonScrollChanged(inthoriz, intvert, intoldHoriz, intoldVert) {
super.onScrollChanged(horiz,vert,oldHoriz,oldVert);
if(vert ==mOffsetHeight|| vert ==0) {
//这里触发父布局或祖父布局的滑动事件
getParent().requestDisallowInterceptTouchEvent(false);
mBottomFlag=true;
}
}
}