Stop ScrollView from auto-scrolling to an EditText

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

    I had a slightly different objection to this infuriating deficiency. Whenever I tapped one of a number of RadioButtons below the EditTexts, the scroll position jumped to accommodate what Android determined to be the visible and focused EditText.

    All attempts to retain the current desired scroll position via a Runnable that issued ScrollView.scrollTo(x,y) were dutifully IGNORED by Android!

    I share my solution in the hope that it may save someone else 8 (eight) wasted hours.

    /* This interesting little 'hack' prevents unwanted scroll 'jump' occurring when
     user touches a RadioButton for example
    [ Causes focus to change - but maybe this is a lesser evil! ] */
    mScrollView.setOnTouchListener(new View.OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() != MotionEvent.ACTION_UP) 
                return false;
    
            mScrollView.clearFocus();
            return false;
        }
    });
    

提交回复
热议问题