我在我的应用程序中实现了新的 SwipeRefreshLayout
组件,它适用于任何垂直视图,如 ListView
、GridView
和 ScrollView
。
它在水平视图(如水平滚动视图
)上表现得非常糟糕。向右或向左滚动时,轻扫刷新布局
视图会缓存触摸,阻止水平滚动视图
接收触摸,并开始垂直滚动以执行刷新。
我尝试解决这个问题,就像我以前解决带有< code>ViewPager的垂直< code>ScrollView的问题一样,使用< code > requestdisallownintercepttouchevent ,但是不起作用。我还注意到这个方法在原来的< code>SwipeRefreshLayout类中被覆盖,而没有返回super。谷歌的开发者留下了一条评论,而不是“<代码>//没有。 ":)
因为SwipeRefreshLayout
组件相对较新,所以我找不到一个解决方案来解决水平滚动问题,同时仍然允许滑动刷新视图来跟踪和处理垂直滚动,所以我想我将与大家分享我的解决方案,希望它能为人们节省一两个小时的时间。
我通过扩展< code>SwipeRefreshLayout并覆盖其< code > oninterceptouchevent 解决了这个问题。在里面,我计算用户漫游的X距离是否大于触摸坡度。如果是,这意味着用户在水平滑动,因此我返回< code>false,让子视图(本例中为< code > HorizontalScrollView )获得触摸事件。
public class CustomSwipeToRefresh extends SwipeRefreshLayout {
private int mTouchSlop;
private float mPrevX;
public CustomSwipeToRefresh(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPrevX = MotionEvent.obtain(event).getX();
break;
case MotionEvent.ACTION_MOVE:
final float eventX = event.getX();
float xDiff = Math.abs(eventX - mPrevX);
if (xDiff > mTouchSlop) {
return false;
}
}
return super.onInterceptTouchEvent(event);
}
}
如果您不记住您已经拒绝了ACTION_MOVE事件的事实,那么如果用户回到您最初的 mPrevX 附近,您最终会稍后接受它。
加个布尔就背下来了。
public class CustomSwipeToRefresh extends SwipeRefreshLayout {
private int mTouchSlop;
private float mPrevX;
// Indicate if we've already declined the move event
private boolean mDeclined;
public CustomSwipeToRefresh(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPrevX = MotionEvent.obtain(event).getX();
mDeclined = false; // New action
break;
case MotionEvent.ACTION_MOVE:
final float eventX = event.getX();
float xDiff = Math.abs(eventX - mPrevX);
if (mDeclined || xDiff > mTouchSlop) {
mDeclined = true; // Memorize
return false;
}
}
return super.onInterceptTouchEvent(event);
}
}
Lior Iluz提出的覆盖onInterceptTouchEvent()的解决方案有一个严重的问题。如果内容可滚动容器未完全向上滚动,则可能无法在同一向上滚动手势中激活轻扫以刷新。事实上,当您开始滚动内部容器并无意中将手指水平移动而不是mTouchSlop(默认为8dp)时,建议的CustomSwipeToRefresh会拒绝此手势。因此,用户必须再次尝试才能开始刷新。对于用户来说,这可能看起来很奇怪。
我将原始 SwipeRefreshLayout 的源代码从支持库提取到我的项目中,并重新编写了 onInterceptTouchEvent()。新类名称是 TouchSafeSwipeRefreshLayout
private boolean mPendingActionDown;
private float mInitialDownY;
private float mInitialDownX;
private boolean mGestureDeclined;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
ensureTarget();
final int action = ev.getActionMasked();
int pointerIndex;
if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
mReturningToStart = false;
}
if (!isEnabled() || mReturningToStart || mRefreshing ) {
// Fail fast if we're not in a state where a swipe is possible
if (D) Log.e(LOG_TAG, "Fail because of not enabled OR refreshing OR returning to start. "+motionEventToShortText(ev));
return false;
}
switch (action) {
case MotionEvent.ACTION_DOWN:
setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop());
mActivePointerId = ev.getPointerId(0);
if ((pointerIndex = ev.findPointerIndex(mActivePointerId)) >= 0) {
if (mNestedScrollInProgress || canChildScrollUp()) {
if (D) Log.e(LOG_TAG, "Fail because of nested content is Scrolling. Set pending DOWN=true. "+motionEventToShortText(ev));
mPendingActionDown = true;
} else {
mInitialDownX = ev.getX(pointerIndex);
mInitialDownY = ev.getY(pointerIndex);
}
}
return false;
case MotionEvent.ACTION_MOVE:
if (mActivePointerId == INVALID_POINTER) {
if (D) Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
return false;
} else if (mGestureDeclined) {
if (D) Log.e(LOG_TAG, "Gesture was declined previously because of horizontal swipe");
return false;
} else if ((pointerIndex = ev.findPointerIndex(mActivePointerId)) < 0) {
return false;
} else if (mNestedScrollInProgress || canChildScrollUp()) {
if (D) Log.e(LOG_TAG, "Fail because of nested content is Scrolling. "+motionEventToShortText(ev));
return false;
} else if (mPendingActionDown) {
// This is the 1-st Move after content stops scrolling.
// Consider this Move as Down (a start of new gesture)
if (D) Log.e(LOG_TAG, "Consider this move as down - setup initial X/Y."+motionEventToShortText(ev));
mPendingActionDown = false;
mInitialDownX = ev.getX(pointerIndex);
mInitialDownY = ev.getY(pointerIndex);
return false;
} else if (Math.abs(ev.getX(pointerIndex) - mInitialDownX) > mTouchSlop) {
mGestureDeclined = true;
if (D) Log.e(LOG_TAG, "Decline gesture because of horizontal swipe");
return false;
}
final float y = ev.getY(pointerIndex);
startDragging(y);
if (!mIsBeingDragged) {
if (D) Log.d(LOG_TAG, "Waiting for dY to start dragging. "+motionEventToShortText(ev));
} else {
if (D) Log.d(LOG_TAG, "Dragging started! "+motionEventToShortText(ev));
}
break;
case MotionEvent.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mIsBeingDragged = false;
mGestureDeclined = false;
mPendingActionDown = false;
mActivePointerId = INVALID_POINTER;
break;
}
return mIsBeingDragged;
}
请参阅我在 Github 上的示例项目。