解决EditText在ScrollView中获取焦点自动滚动问题

问题:

在实际开发中会遇到表单页面,表单中有很多选项,包括EditText等,这样会导致一屏显示不下,我们通常都会添加一个ScrollView使其页面可以上下滑动。

 <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <LinearLayout
               android:layout_width="match_parent"
                 android:layout_height="match_parent">
        ......
         <EditText
             android:id="@+id/game_title"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>
        ......
         </LinearLayout>
</ScrollView>

当我们点击输入框获取焦点后,滑动页面去操作其他内容后,这时ScrollView会自动滚动到获取焦点的输入框位置,这样会用户体验非常不好。

Google到的一些方法:

在EditText父布局添加descendantFocusability

android:descendantFocusability="blocksDescendants"

在EditText父布局添加focusable,focusableInTouchMode

android:focusable="true"  
android:focusableInTouchMode="true"

stackoverflow链接

scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS)
scrollView.setFocusable(true)
scrollView.setFocusableInTouchMode(true)
scrollView.setOnTouchListener({ v, event ->
      v.requestFocusFromTouch()
      false

最后这些方法都不能解决我们的问题,没办法只能通过查看源码来看看有没有什么办法解决了。

源码分析:

一般一个view获取焦点的方式有两中方式,一种是requestFocus,另一种是requestFocusFromTouch,两个方法的区别网上都有很多 链接,requestFocusFromTouch最终还是会调用requestFocus方法,所以我进入requestFocus方法,一步一步往下走最终会调用handleFocusGainInternal方法

void handleFocusGainInternal(@FocusRealDirection int direction, Rect previouslyFocusedRect) {
       ......
        if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
            mPrivateFlags |= PFLAG_FOCUSED;
            View oldFocus = (mAttachInfo != null) ? getRootView().findFocus() : null;
            if (mParent != null) {
                mParent.requestChildFocus(this, this);
                updateFocusedInCluster(oldFocus, direction);
             ......

这里会看到mParent.requestChildFocus,这里的mParent是ViewParent类的对象,ViewParent是一个接口,所有的ViewGroup都会实现它,这里我们使用的ScrollView,所以这里查看ScrollView的requestChildFocus实现。

@Override
    public void requestChildFocus(View child, View focused) {
        if (focused != null && focused.getRevealOnFocusHint()) {
            if (!mIsLayoutDirty) {
                scrollToChild(focused);
            } else {
                // The child may not be laid out yet, we can't compute the scroll yet
                mChildToScrollTo = focused;
        super.requestChildFocus(child, focused);

当获取到焦点并且视图已经执行onLayout时调用scrollToChild方法,这个方法就是页面滚动的方法。

private void scrollToChild(View child) {
        child.getDrawingRect(mTempRect);
        /* Offset from child's local coordinates to ScrollView coordinates */
        offsetDescendantRectToMyCoords(child, mTempRect);
        int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
        if (scrollDelta != 0) {
            scrollBy(0, scrollDelta);

这段代码主要是获取view的位置,然后通过computeScrollDeltaToGetChildRectOnScreen计算出需要滚动的距离,然后调用scrollBy方法滚动到已获得焦点View的位置。看到这里我们可以通过重写computeScrollDeltaToGetChildRectOnScreen方法然后返回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;