PS:
//加上这一句可以让横向的RecyclerVIew实现ViewPager的效果
PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
pagerSnapHelper.attachToRecyclerView(mRvList);
//内层RecyclerView
public class KidRecyclerView extends RecyclerView {
private static final int INVALID_POINTER = -1;
private int mScrollPointerId = INVALID_POINTER;
private int mInitialTouchX, mInitialTouchY;
private int mTouchSlop;
public KidRecyclerView(Context context) {
this(context, null);
}
public KidRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public KidRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ViewConfiguration vc = ViewConfiguration.get(getContext());
mTouchSlop = vc.getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
final int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mScrollPointerId = e.getPointerId(0);
mInitialTouchX = (int) (e.getX());
mInitialTouchY = (int) (e.getY());
return super.onInterceptTouchEvent(e);
case MotionEvent.ACTION_MOVE: {
final int index = e.findPointerIndex(mScrollPointerId);
if (index < 0) {
return false;
}
final int x = (int) (e.getX(index));
final int y = (int) (e.getY(index));
if (getScrollState() != SCROLL_STATE_DRAGGING) {
final int dx = x - mInitialTouchX;
final int dy = y - mInitialTouchY;
final boolean canScrollHorizontally = getLayoutManager().canScrollHorizontally();
final boolean canScrollVertically = getLayoutManager().canScrollVertically();
boolean startScroll = false;
if (canScrollHorizontally && Math.abs(dx) > mTouchSlop && (Math.abs(dx) >= Math.abs(dy) || canScrollVertically)) {
startScroll = true;
}
if (canScrollVertically && Math.abs(dy) > mTouchSlop && (Math.abs(dy) >= Math.abs(dx) || canScrollHorizontally)) {
startScroll = true;
}
return startScroll && super.onInterceptTouchEvent(e);
}
return super.onInterceptTouchEvent(e);
}
default:
return super.onInterceptTouchEvent(e);
}
}
}
//外层RecyclerView
public class FatherRecyclerView extends BetterRecyclerView {
public FatherRecyclerView(Context context) {
this(context, null);
}
public FatherRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public FatherRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
//这里什么都不做
}
}