Recycler view inside NestedScrollView causes scroll to start in the middle

后端 未结 11 1007
眼角桃花
眼角桃花 2020-12-12 10:07

I am getting a weird scrolling behavior when I add a RecyclerView inside a NestedScrollView.

What happens is that whenever the scrollview has more rows than can be s

11条回答
  •  北海茫月
    2020-12-12 10:22

    I had the same issue and sovled by extending NestedScrollView and disabling focusing children. For some reason, RecyclerView always requested focus even when I just opened and closed the drawer.

    public class DummyNestedScrollView extends NestedScrollView {
    public DummyNestedScrollView(Context context) {
        super(context);
    }
    
    public DummyNestedScrollView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    
    public DummyNestedScrollView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    /**
     * Fixind problem with recyclerView in nested scrollview requesting focus
     * http://stackoverflow.com/questions/36314836/recycler-view-inside-nestedscrollview-causes-scroll-to-start-in-the-middle
     * @param child
     * @param focused
     */
    @Override
    public void requestChildFocus(View child, View focused) {
        Log.d(getClass().getSimpleName(), "Request focus");
        //super.requestChildFocus(child, focused);
    
    }
    
    
    /**
     * http://stackoverflow.com/questions/36314836/recycler-view-inside-nestedscrollview-causes-scroll-to-start-in-the-middle
     * @param direction
     * @param previouslyFocusedRect
     * @return
     */
    @Override
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
        Log.d(getClass().getSimpleName(), "Request focus descendants");
        //return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
        return false;
    }
    }
    

提交回复
热议问题