Android EditText's container (RecyclerView) auto scroll when EditText is Focused

♀尐吖头ヾ 提交于 2019-12-01 21:02:22

Actually, there is a scroll state of RecycleView that you can control like this;

Create your own LayoutManager and override the scrollHorizontallyBylike this.

@Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int nScroll = 0;
        // Do not let auto scroll 
        if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_SETTLING){
          nScroll = super.scrollHorizontallyBy(dx, recycler, state);
        } 
}

So, what is the SCROLL_STATE_SETTLING?

The RecyclerView is currently animating to a final position while not under outside control.

I finally fixed it by disable the RecyclerView from scrolling except it receive a touch event.

first, I custom a LayoutManager:

@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {

    if(!horizontal) {
        return 0;
    }

@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
    if(!vertical) {
        return 0;
    }

when receive click event : I set horizontal and vertical to false ,these cause RecyclerView cannot scroll any more!

and I subClass The recyclerView and Override onTouchEvent:

    public boolean onTouchEvent(MotionEvent e) {

    //we enable scrollHorizontallyBy and scrollVerticallyBy only in Touch Event, set layoutManager vertical and horizontal to true
    ......

    return super.onTouchEvent(e);
}

so MyRecyclerView cannot scroll when click to find that the focus child is an EditText. But it can scroll when Receive Touch Event!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!