滑動(dòng)沖突場(chǎng)景:外層橫向滑動(dòng),內(nèi)層豎向滑動(dòng)实束。這里通過(guò)內(nèi)層攔截的方式來(lái)處理沖突。
注意:例子中得布局是ViewGroupOutside2包裹兩個(gè)RecyclerView和兩個(gè)MyTextView,橫向排列煤辨。TextView內(nèi)部是處理點(diǎn)擊事件的竞滓,所以自定義MyTextView繼承TextView,將onTouchEvent()的返回修改為false,那么ViewGroupOutside2就可以接收到move和up事件疫向,可以實(shí)現(xiàn)橫向滑動(dòng)咳蔚。接下來(lái)我們自定義MyRecyclerView;
1.MyRecyclerView
/**
* Created by dingmouren
* email: naildingmouren@gmail.com
* github: https://github.com/DingMouRen
* 內(nèi)部攔截法
*/
public class MyRecyclerView extends RecyclerView {
/*分別記錄上次滑動(dòng)的坐標(biāo)*/
private int mLastX = 0;
private int mLastY = 0;
public MyRecyclerView(@NonNull Context context) {
super(context);
}
public MyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
/*按下時(shí)設(shè)置父布局,不攔截*/
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
int deltaX = x - mLastX;
int deltaY = y - mLastY;
/*父布局經(jīng)過(guò)上面按下時(shí)不攔截的標(biāo)志位搔驼,子view能接收到move,如果是橫向滑動(dòng)谈火,設(shè)置父布局進(jìn)行攔截*/
if (Math.abs(deltaX) > Math.abs(deltaY)){
getParent().requestDisallowInterceptTouchEvent(false);
}
break;
case MotionEvent.ACTION_UP:
break;
}
mLastX = x;
mLastY = y;
return super.dispatchTouchEvent(ev);
}
}
2.ViewGroupOutside2需要修改,對(duì)onIntercepterTouchEvent進(jìn)行處理,除了ACTION_DOWN不攔截舌涨,其他的動(dòng)作都要攔截糯耍。
/**
* Created by dingmouren
* email: naildingmouren@gmail.com
* github: https://github.com/DingMouRen
* 內(nèi)部攔截法
*/
public class ViewGroupOutside2 extends ViewGroup {
private static final String TAG = "ViewGroupOutside2";
private int mChildrenSize;
private int mChildWidth;
private int mChildIndex;
/*分別記錄上次滑動(dòng)的坐標(biāo)*/
private int mLastX = 0;
private int mLastY = 0;
/*分別記錄上次滑動(dòng)的坐標(biāo)(onInterceptTouchEvent)*/
private int mLastXIntercept = 0;
private int mLastYIntercept = 0;
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
public ViewGroupOutside2(Context context) {
this(context,null);
}
public ViewGroupOutside2(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}
public ViewGroupOutside2(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mScroller = new Scroller(getContext());
mVelocityTracker = VelocityTracker.obtain();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/*測(cè)量所有子view*/
measureChildren(widthMeasureSpec,heightMeasureSpec);
/*測(cè)量自己*/
measureSelf(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
mChildrenSize = childCount;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
mChildWidth = childWidth;
child.layout(childWidth * i,0,childWidth * (i + 1),childHeight);
}
}
/**
* 內(nèi)部攔截法
* @param ev
* @return
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int x = (int) ev.getX();
int y = (int) ev.getY();
if (ev.getAction() == MotionEvent.ACTION_DOWN){
mLastX = x;
mLastY = y;
if (!mScroller.isFinished()){/*橫向滑動(dòng)還沒(méi)結(jié)束時(shí),攔截事件*/
mScroller.abortAnimation();
return true;
}
return false;
}else {
return true;
}
}
/**
* 橫向滑動(dòng)得處理
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
mVelocityTracker.addMovement(event);
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()){
mScroller.abortAnimation();
}
break;
case MotionEvent.ACTION_MOVE:
int deltax = x - mLastX;
int deltay = y - mLastY;
scrollBy(-deltax,0);
break;
case MotionEvent.ACTION_UP:
int scrollX = getScrollX();
int scrollToChildIndex = scrollX / mChildWidth;
mVelocityTracker.computeCurrentVelocity(1000);
float xVelocity = mVelocityTracker.getXVelocity();
if (Math.abs(xVelocity) >= 50){
mChildIndex = xVelocity > 0 ? mChildIndex - 1 : mChildIndex + 1;
}else {
mChildIndex = (scrollX + mChildWidth / 2) / mChildWidth;
}
mChildIndex = Math.max(0,Math.min(mChildIndex,mChildrenSize - 1));
int dx = mChildIndex * mChildWidth - scrollX;
smoothScrollBy(dx,0);
mVelocityTracker.clear();
break;
}
mLastX = x;
mLastY = y;
return super.onTouchEvent(event);
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()){
scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
postInvalidate();
}
}
/**
* 滑動(dòng)
* @param dx
* @param dy
*/
private void smoothScrollBy(int dx,int dy){
mScroller.startScroll(getScrollX(),0,dx,0,500);
invalidate();
}
private void measureSelf(int widthMeasureSpec,int heightMeausreSpec){
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeausreSpec);
int heightSize = MeasureSpec.getSize(heightMeausreSpec);
int width = 0;
int height = 0;
switch (widthMode){
case MeasureSpec.EXACTLY:
width = widthSize;
break;
case MeasureSpec.AT_MOST:
width = widthSize;
break;
case MeasureSpec.UNSPECIFIED:
break;
}
switch (heightMode){
case MeasureSpec.EXACTLY:
height = heightSize;
break;
case MeasureSpec.AT_MOST:
height = heightSize;
break;
case MeasureSpec.UNSPECIFIED:
break;
}
setMeasuredDimension(width,height);
}
}