ScrollView嵌套多行的EditText时,EditText内容滑动不了。
et.setOnTouchListener((view, motionEvent) -> {
if ((view.getId() == R.id.et && canVerticalScroll(et))) {
view.getParent().requestDisallowInterceptTouchEvent(true);
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
view.getParent().requestDisallowInterceptTouchEvent(false);
}
}
return false;
});
// 判断EditText是否可滑动
private boolean canVerticalScroll(EditText editText) {
int scrollY = editText.getScrollY();
int scrollRange = editText.getLayout().getHeight();
int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() - editText.getCompoundPaddingBottom();
int scrollDifference = scrollRange - scrollExtent;
if(scrollDifference == 0) {
return false;
}
return (scrollY > 0) || (scrollY < scrollDifference - 1);
}