public class TopFadingEdgeRecyclerView extends RecyclerView {
public static final String TAG = "chenhaocc";
private float downX, downY;
private static final float SLIDE_ANGLE = 45;
private static final int UP = -1;
private static final int DOWN = 1;
public TopFadingEdgeRecyclerView(@NonNull @NotNull Context context) {
super(context);
}
public TopFadingEdgeRecyclerView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
super(context, attrs);
}
public TopFadingEdgeRecyclerView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected float getTopFadingEdgeStrength() {
return 0.5f;
}
@Override
protected float getBottomFadingEdgeStrength() {
return 0;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = ev.getX();
downY = ev.getY();
// requestDisallowInterceptTouchEvent true 自己消费,false 父类消费
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
float moveX = ev.getX();
float moveY = ev.getY();
float xDiff = Math.abs(moveX - downX);
float yDiff = Math.abs(moveY - downY);
double squareRoot = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
//滑动的角度
int yAngle = Math.round((float) (Math.asin(yDiff / squareRoot) / Math.PI * 180));
int xAngle = Math.round((float) (Math.asin(xDiff / squareRoot) / Math.PI * 180));
boolean isMeetSlidingYAngle = yAngle > SLIDE_ANGLE;//滑动角度是否大于45du
boolean isMeetSlidingXAngle = xAngle > SLIDE_ANGLE;//滑动角度是否大于45du
boolean isSlideUp = moveY < downY && isMeetSlidingYAngle;
boolean isSlideDown = moveY > downY && isMeetSlidingYAngle;
boolean isSlideLeft = moveX < downX && isMeetSlidingXAngle;
boolean isSlideRight = moveX > downX && isMeetSlidingXAngle;
if (isSlideUp) {
Log.d(TAG, "向上滑动");
// 是否滑动到底部了
boolean isBottom = !canScrollVertically(DOWN);
Log.d(TAG, "isBottom:" + isBottom);
// 父类不拦截,自己消费 没用滑动到底部,自己消费
getParent().requestDisallowInterceptTouchEvent(!isBottom);
} else if (isSlideDown) {
Log.d(TAG, "向下滑动");
// 是否到顶了
boolean isTop = !canScrollVertically(UP);
Log.d(TAG, "isTop:" + isTop);
if (isTop) {
if (getParent() instanceof SwipeRefreshLayout) {
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) getParent();
boolean hasLoadMore = swipeRefreshLayout.isEnabled();
// 能加载更多,父类不拦截,自己消费
getParent().requestDisallowInterceptTouchEvent(hasLoadMore);
} else {
getParent().requestDisallowInterceptTouchEvent(false);
}
} else {
getParent().requestDisallowInterceptTouchEvent(true);
}
} else if (isSlideLeft) {
Log.d(TAG, "向左边滑动");
getParent().requestDisallowInterceptTouchEvent(false);
} else if (isSlideRight) {
getParent().requestDisallowInterceptTouchEvent(false);
Log.d(TAG, "向右边滑动");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
getParent().requestDisallowInterceptTouchEvent(false);
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
}
Android 重写recycleView解决与竖版viewpager冲突
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...