我有一个包含多个Fragment的ViewPager。
每个Fragment中都有一个SwipeRefreshLayout,其中包含一个拖动条。
然而,拖动条的滑块不平滑地移动(水平方向)。
滑块总是移动一定百分比的进度,并且即使我仍在触摸滑块,也会自动释放触摸。
有人遇到过相同的情况吗?
如何解决这个问题?
我有一个包含多个Fragment的ViewPager。
每个Fragment中都有一个SwipeRefreshLayout,其中包含一个拖动条。
然而,拖动条的滑块不平滑地移动(水平方向)。
滑块总是移动一定百分比的进度,并且即使我仍在触摸滑块,也会自动释放触摸。
有人遇到过相同的情况吗?
如何解决这个问题?
@robxyy的回答是正确的,但我花了一段时间才弄清楚实现细节。因此,在详细说明他的答案之前,请查看下面的代码:
public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
private int mTouchSlop;
private float mPrevX;
private boolean isDisabled;
public CustomSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public void disableInterceptTouchEvent(boolean isDisabled) {
this.isDisabled = isDisabled;
getParent().requestDisallowInterceptTouchEvent(isDisabled);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPrevX = MotionEvent.obtain(event).getX();
break;
case MotionEvent.ACTION_MOVE:
if (isDisabled) {
return false;
}
float eventX = event.getX();
float xDiff = Math.abs(eventX - mPrevX);
if (xDiff > mTouchSlop) {
return false;
}
}
return super.onInterceptTouchEvent(event);
}
}
disableInterceptTouchEvent(true)
disableInterceptTouchEvent(false)
public class SwipeView extends RelativeLayout implements SeekBar.OnSeekBarChangeListener {
private CustomSwipeRefreshLayout swipeRefreshLayout;
public SwipePromotionView(CustomSwipeRefreshLayout swipeRefreshLayout) {
this.swipeRefreshLayout = swipeRefreshLayout;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// include some logic here to decide which part of the swipe bar is a valid starting point
swipeRefreshLayout.disableInterceptTouchEvent(true);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
swipeRefreshLayout.disableInterceptTouchEvent(false);
}
}
public void disableInterceptTouchEvent(boolean isDisabled) { this.isDisabled = isDisabled; getParent().requestDisallowInterceptTouchEvent(isDisabled); }
@Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mPrevX = MotionEvent.obtain(event).getX(); break; case MotionEvent.ACTION_MOVE: if (isDisabled) { return false; } float eventX = event.getX(); float xDiff = Math.abs(eventX - mPrevX); if (xDiff > mTouchSlop) { return false; } } return super.onInterceptTouchEvent(event); }