Stop ScrollView from auto-scrolling to an EditText

后端 未结 21 1350
长发绾君心
长发绾君心 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:35

    My fix to this most horrific bug, (worth noting that this is pre API11 only where they modified the fling method not to be stupid).

    The old fling method finds the next focus that it will get to.. which isn't really that helpful. Other versions of this class don't really work as they stop focus working when the user genuinely traverses the form from the keyboard.

    public class NonFocusingScrollView extends ScrollView {
    
        private boolean mBlockRequestFocusOnFling = false;
    
        public NonFocusingScrollView(Context context) {
            super(context);
        }
    
        public NonFocusingScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public NonFocusingScrollView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public ArrayList getFocusables(int direction) {
            if(mBlockRequestFocusOnFling)
                return new ArrayList();
            return super.getFocusables(direction);
        }
    
        @Override
        public void requestChildFocus(View child, View focused) {
            if(!mBlockRequestFocusOnFling)
            super.requestChildFocus(child, focused);
        }
    
    
        @Override
        public void fling(int velocityY) {
            mBlockRequestFocusOnFling = true;
            super.fling(velocityY);
            mBlockRequestFocusOnFling = false;
        }
    }
    

提交回复
热议问题