Skip to content Skip to sidebar Skip to footer

Android Edittext's Container (recyclerview) Auto Scroll When Edittext Is Focused

I got a RecyclerView whose element contains a EditText widget. 1.currently focusing on the second EditText,the first EditText is partly showed on the screen. the first EditText is

Solution 1:

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

Create your own LayoutManager and override the scrollHorizontallyBylike this.

@OverridepublicintscrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        intnScroll=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.

Solution 2:

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

first, I custom a LayoutManager:

@OverridepublicintscrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {

    if(!horizontal) {
        return0;
    }

@OverridepublicintscrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
    if(!vertical) {
        return0;
    }

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:

publicbooleanonTouchEvent(MotionEvent e) {

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

    returnsuper.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!

Post a Comment for "Android Edittext's Container (recyclerview) Auto Scroll When Edittext Is Focused"