ScrollView disable focus move

后端 未结 10 673
野性不改
野性不改 2020-12-14 16:36

I have a simple input form; it\'s a vertical LinearLayout with EditTexts inside a ScrollView.



        
10条回答
  •  失恋的感觉
    2020-12-14 17:04

    What worked for me was combining @dmon's and @waj's answers.

    Only overriding onRequestFocusInDescendants() worked great when I was only dealing with EditTexts inside of the ScrollView, but when I started added multiple View types, it didn't work so well.

    Only overriding getFocusables() did not work at all.

    Overriding both onRequestFocusInDescendants() AND getFocusables() seems to work beautifully in all scenarios.

    public class FixedFocusScrollView extends ScrollView {
    
      public FixedFocusScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
      }
    
      public FixedFocusScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      public FixedFocusScrollView(Context context) {
        super(context);
      }
    
      @Override
      public ArrayList getFocusables(int direction) {
          return new ArrayList();
      }
    
      @Override
      protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
        return true;
      }
    
    }
    

提交回复
热议问题