ScrollView disable focus move

后端 未结 10 582
野性不改
野性不改 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:15

    This method is very effective,you can overload computeScrollDeltaToGetChildRectOnScreen and return 0

    public class NoScrollFocusScrollView extends ScrollView {
    
        public NoScrollFocusScrollView(Context context) {
            super(context);
        }
    
        public NoScrollFocusScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public NoScrollFocusScrollView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
            return 0;
        }
    }
    
    
    0 讨论(0)
  • 2020-12-14 17:16

    Happened to me today, basically I was adding a View programmatically between some existing views, and Scroll automatically moved to focus that view. What I just did, is the following:

    container.setFocusable( false );
    
    0 讨论(0)
  • 2020-12-14 17:18

    I found another easy solution that works for my problem.

    I got a ScrollView with an EditText at the top, and after it, a big list of TextViews, RatingBars and Buttons. The Buttons launch AlertDialogs, and when they pops up, the ScrollView moves to the top, to the EditText that is the one that still has the focus.

    To solve it, I set in the onClick method of the Activity the requestFocusFromTouch() to the view clicked (in this case the Button).

    public void onClick(View v) {
        v.requestFocusFromTouch();
        ...
    }
    

    So now when I click on a Button, the ScrollView moves and put that Button on the center of the screen, that was just what I wanted.

    I hope it help.

    0 讨论(0)
  • 2020-12-14 17:21

    Try to cut the problem from the source (edit: i.e. move it to an XML file).

    First, there must be a focusable element for that to happen. Make all focusable elements contained in that scroll into non-focusable elements. Make them focusable after the view is inflated and is visible.

    android:focusable="false"
    android:focusableInTouchMode="false"
    
    0 讨论(0)
提交回复
热议问题