Why does my android activity always start scrolled to the bottom?

前端 未结 11 901
萌比男神i
萌比男神i 2020-12-22 20:56

Whenever I start this activity, it always starts bottomed out--scrolled all the way to the bottom. I am not doing anything weird in the activity OnCreate (or anywhere for th

相关标签:
11条回答
  • 2020-12-22 20:56

    Instead of polluting items in your layout, you can add this into your Activity/Class:

        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
        scrollView.setFocusableInTouchMode(true); 
        scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
    

    This way it isn't an element within your scrollView getting focus, it's your scrollView as a container which get's the first focus, moving onto the child view's after this.

    0 讨论(0)
  • 2020-12-22 20:59

    In my case I had a VideoView inside a ConstraintLayout inside a ScrollView. The VideoView was always stealing focus, and there are issues with it. Solution is to use a TextureView. Hopefully this helps someone, it cost me an hour at least :)

    0 讨论(0)
  • 2020-12-22 21:01

    Just make it not focusable in onCreate()

    editText.setFocusable(false);
    

    and use

    editText.setOnClickListener(new ....){
     ......
      editText.setFocusable(true);
      editText.setFocusableInTouchMode(true);
      editText.requestFocus();
    
      showSoftKeyboard(); //Use InputMethodManager to open soft Keyboard
    
     ......
     };
    

    and it will not scroll down to Edittext next time, a fragment or activity is created.

    0 讨论(0)
  • 2020-12-22 21:03

    scrollView.requestFocus(View.FOCUS_UP) solved my problem when I wanted the top view to be visible. Specifying direction parameter to the method requestFocus(int direction) is a good way to control the ScrollView position. More details here.

    0 讨论(0)
  • 2020-12-22 21:06

    Have you tried use fullScroll method ?http://developer.android.com/reference/android/widget/ScrollView.html#fullScroll(int)

    yourScrollView.fullScroll(ScrollView.FOCUS_UP);
    
    0 讨论(0)
  • 2020-12-22 21:09

    When Android starts an activity, some control needs to take focus. When there's no designated control to take focus, the system chooses the first eligible control that wants focus. If you set the following property in your LinearLayout - android:focusableInTouchMode="true" your LinearLayout will be focused on start and your activity won't scroll to EditText in the bottom.

    0 讨论(0)
提交回复
热议问题