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

后端 未结 2 1249
暗喜
暗喜 2021-01-19 12:49

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 fir

2条回答
  •  深忆病人
    2021-01-19 13:24

    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!

提交回复
热议问题