- 滚动冲突解决
public class XEditText extends EditText {
//滑动距离的最大边界
private int mOffsetHeight;
//是否到顶或者到底的标志
private boolean mBottomFlag = false;
public XEditText(Context context) {
super(context);
}
public XEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public XEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int paddingTop;
int paddingBottom;
int height;
int layoutHeight;
//获得内容面板
Layout layout = getLayout();
//获得内容面板的高度
layoutHeight = layout.getHeight();
//获取上内边距
paddingTop = getTotalPaddingTop();
//获取下内边距
paddingBottom = getTotalPaddingBottom();
//获得控件的实际高度
height = getHeight();
//计算滑动距离的边界
mOffsetHeight = layoutHeight + paddingTop + paddingBottom - height;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
//如果是新的按下事件,则对mBottomFlag重新初始化
mBottomFlag = false;
//如果已经不要这次事件,则传出取消的信号,这里的作用不大
if (mBottomFlag)
event.setAction(MotionEvent.ACTION_CANCEL);
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = super.onTouchEvent(event);
//如果是需要拦截,则再拦截,这个方法会在onScrollChanged方法之后再调用一次
if (!mBottomFlag)
getParent().requestDisallowInterceptTouchEvent(true);
return result;
}
@Override
protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
super.onScrollChanged(horiz, vert, oldHoriz, oldVert);
if (vert == mOffsetHeight || vert == 0) {
//这里触发父布局或祖父布局的滑动事件
getParent().requestDisallowInterceptTouchEvent(false);
mBottomFlag = true;
}
}
}
- 解决EditText换行后,ScrollView跟随滚动的问题
重写ScrollView
的computeScrollDeltaToGetChildRectOnScreen
方法
@Override
protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
View currentFocused = findFocus();
if (null == currentFocused || this == currentFocused) {
return super.computeScrollDeltaToGetChildRectOnScreen(rect);
}
// 重新计算需要显示的区域
Rect tempRect = new Rect();
currentFocused.getDrawingRect(tempRect);
offsetDescendantRectToMyCoords(currentFocused, tempRect);
return super.computeScrollDeltaToGetChildRectOnScreen(tempRect);
}