方法一: 将NestedScrollView 替换为ScrollView即可
方法二: 重写NestedScrollView
第一步: 禁止RecyclerView的滑动
recyclerView.setNestedScrollingEnabled(false);
第二步: 重写NestedScrollView
public class RVNestedScrollView extends NestedScrollView {
private int mDownX;
private int mDownY;
private int mTouchSlop;
public RVNestedScrollView(Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public RVNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public RVNestedScrollView(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:
mDownX = (int) e.getRawX();
mDownY = (int) e.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) e.getRawY();
if (Math.abs(moveY-mDownY) > mTouchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(e);
}
}
第三步: 在布局文件RVNestedScrollView添加 android:fillViewport="true"属性.并将RecyclerView的layout_height属性和RecyclerView的item_layout最外层的容器layout_height都设置为wrap_content即可.