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;
    }
}

扩展:

scrollToChild方法的下面还有个scrollToChildRect方法,这两个方法作用很相似,这个方法是什么时候调用的呢。当我们操作页面的控件显示或隐藏调用view.setVisibility()方法时,页面重新绘制,会调用doTraversal、performTraversals方法,最终会调用到scrollToChildRect方法,所以我们调用setVisibility时也会导致页面发生滚动,这里我们重写了computeScrollDeltaToGetChildRectOnScreen方法就不会导致此问题发生了。这里就不具体分析了,有兴趣的可以自己查看源码。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值