Stop ScrollView from auto-scrolling to an EditText

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

    I often has this problem when my apps handle orientation change.

    In that case I use the following kind of code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        // to avoid the scrollview to scroll to this element automatically
        mEditTextSearch.setFocusable(false);
        // Get the saved scroll position
        final int scrolly = savedInstanceState.getInt("scrolly");
        mScrollView.post(new Runnable() {
            @Override
            public void run() {
                mScrollView.scrollTo(0, scrolly);
                // Restore the initial state of the EditText
                mEditTextSearch.setFocusable(true);
                mEditTextSearch.setFocusableInTouchMode(true);
                mEditTextSearch.setClickable(true);
            }
        });
        ...
    }
    

提交回复
热议问题