Stop ScrollView from auto-scrolling to an EditText

后端 未结 21 1286
长发绾君心
长发绾君心 2020-11-30 23:17

Seems to be a common problem without a great solution that I have found. Goal is to stop a ScrollView from auto-scrolling to an EditText (or any vi

21条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 23:36

    Try this one :)

    public class CustomHorizontalScrollView extends HorizontalScrollView {
    
        public CustomHorizontalScrollView(Context context) {
        super(context);
        }
    
        public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        }
    
        public CustomHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(ev);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
        }
    
        @Override
        public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
        }
    
    
        //Custom smooth scroll method since norm is final and cannot be overridden
        public final void smooothScrollToIfEnabled(int x, int y) {
        smoothScrollTo(x, y);
        }
    
        @Override
        protected int computeScrollDeltaToGetChildRectOnScreen(android.graphics.Rect rect) {
    /*  if (getContext() != null && getContext() instanceof Activity) {
            Activity activity = (Activity) getContext();
            if (!activity.isFinishing()) {
            View view = activity.getCurrentFocus();
            if (view != null) {
                if (view instanceof EditText) {
                return 0;
                }
            }
            }
        }
        return super.computeScrollDeltaToGetChildRectOnScreen(rect);
    
    */
    return 0;
        }
    
    }
    

提交回复
热议问题