方法一:将RecyclerView的可滑动属性设置为false,这里重写他的LayoutManager的canScrollVertically()方法即可(我用的竖直布局)
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false){ @Override public boolean canScrollVertically() { return false; } }; recyclerView.setLayoutManager(manager);
方法二:重写外层的ScrollView,5.1以上系统会默认把滑动事件传递给内层的RecyclerView,我们直接在外层强行拦截就好了
/** * 屏蔽滑动事件 * * 用于在RecyclerView外层嵌套ScrollView * 直接在ScrollView层将竖直的滑动事件拦截下来,避免与内层的RecyclerView滑动冲突 * */ public class MyScrollview extends ScrollView { private int downY; private int mTouchSlop; public MyScrollview(Context context) { this(context,null); } public MyScrollview(Context context, AttributeSet attrs) { this(context,attrs,0); } public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } @Override public boolean onInterceptTouchEvent(MotionEvent e) { int action = e.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: downY = (int) e.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveY = (int) e.getRawY(); if (Math.abs(moveY - downY) > mTouchSlop) { return true; } } return super.onInterceptTouchEvent(e); } }
额,有时候你会发现你的外层的ScrollView起始位置不是在最顶部,很可能是内部的RecyclerView之类的默认获取了焦点,只要
recyclerView.setFocusable(false);就可以了