NestedScrollView 嵌套Recyclerview 导致recyclerview无法滚动

NestedScrollView 嵌套Recyclerview 导致recyclerview无法滚动

这边不是处理两者的滚动冲突


            
        
    

一下是recyclerview的ItemView 也是嵌套一层recycelrView



    

        

            

            

            

                
            

        

        

            
        
    

我遇到的问题是ItemView中的recyclerView无法滚动

自己试了在Item的RecyclerView中监听

 @Override
    public boolean startNestedScroll(int axes) {
        LogUtil.d("开始滚动事件");
        return super.startNestedScroll(axes);
    }

    @Override
    public void onScrollStateChanged(int state) {
        LogUtil.d("滚动事件");
        super.onScrollStateChanged(state);
    }

发现根本没有进入滚动监听

在自定义的RecyclerView中添加

@Override
    public boolean onTouchEvent(MotionEvent e) {
        LogUtil.d("MaxHeightRecyclerView touched" + e.getAction());
        return super.onTouchEvent(e);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        LogUtil.d("MaxHeightRecyclerView onInterceptTouchEvent");
      //  requestDisallowInterceptTouchEvent(true);
        return super.onInterceptTouchEvent(e);
    }

发现以上两个事件都会进入

后来发现 // requestDisallowInterceptTouchEvent(true);

该方法是不允许父View拦截事件

我的需求还有是RecyclerView高度自适应 高度超过一定高度滑动

完整代码:

public class MaxHeightRecyclerView extends RecyclerView {
    private int mMaxHeight;

    public MaxHeightRecyclerView(Context context) {
        super(context);
    }

    public MaxHeightRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, AttributeSet attrs) {
        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightRecyclerView);
        mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightRecyclerView_maxHeight, mMaxHeight);
        arr.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mMaxHeight > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public interface Callback {
        void moveEvent();
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        LogUtil.d("MaxHeightRecyclerView touched" + e.getAction());
        return super.onTouchEvent(e);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        LogUtil.d("MaxHeightRecyclerView onInterceptTouchEvent");
        requestDisallowInterceptTouchEvent(true);
        return super.onInterceptTouchEvent(e);
    }

    @Override
    public boolean startNestedScroll(int axes) {
        LogUtil.d("开始滚动事件");
        return super.startNestedScroll(axes);
    }

    @Override
    public void onScrollStateChanged(int state) {
        LogUtil.d("滚动事件");
        super.onScrollStateChanged(state);
    }
}

结束 还是事件分发问题,记录分享

你可能感兴趣的:(记录)