NestedScrollView嵌套EditText,导致EditText滑动冲突

NestedScrollView嵌套EditText,EditText文本很多时,无法上下滑动:

方法一:

   edittext.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                    //通知父控件不要干扰
                    view.getParent().requestDisallowInterceptTouchEvent(true);
                }
                if(motionEvent.getAction()==MotionEvent.ACTION_MOVE){
                    //通知父控件不要干扰
                    view.getParent().requestDisallowInterceptTouchEvent(true);
                }
                if(motionEvent.getAction()==MotionEvent.ACTION_UP){
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                }
                return false;
            }
        });

缺点:只能触摸EditText外部来滑动;

方法二:

// NestedScrollView嵌套EditText,导致EditText滑动冲突
mBinding.etCheckDesc.setOnTouchListener((view, motionEvent) -> {
    if (view.getId() == R.id.et_check_desc && UiUtil.canVerticalScroll(mBinding.etCheckDesc)) {
        //通知父控件不要干扰
        view.getParent().requestDisallowInterceptTouchEvent(true);
    }
    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        view.getParent().requestDisallowInterceptTouchEvent(false);
    }
    return false;
});
 /**
     * EditText竖直方向是否可以滚动
     * 

* https://www.jb51.net/article/86932.htm * * @param editText 需要判断的EditText * @return true:可以滚动 false:不可以滚动 */ public static 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); }

方法三:自定义EditText控件:
参考:https://www.jianshu.com/p/b352fdee325d

你可能感兴趣的:(代码块,view)