Skip to content

Instantly share code, notes, and snippets.

@TakWolf
Last active July 18, 2016 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakWolf/d9d59a7817858fc404b95894356c06b9 to your computer and use it in GitHub Desktop.
Save TakWolf/d9d59a7817858fc404b95894356c06b9 to your computer and use it in GitHub Desktop.
修复RefreshLayout和ViewPager手势冲突
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
refreshLayout.setEnabled(false);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
refreshLayout.setEnabled(true);
break;
}
return false;
}
});
/**
* 修复RefreshLayout和ViewPager手势冲突,感觉并不怎么好使
* http://stackoverflow.com/questions/23989910/horizontalscrollview-inside-swiperefreshlayout/23989911#23989911
*/
public class SwipeRefreshLayout extends android.support.v4.widget.SwipeRefreshLayout {
private int mTouchSlop;
private float mPrevX;
public SwipeRefreshLayout(Context context) {
super(context);
init(context, null);
}
public SwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet 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:
float xDiff = Math.abs(event.getX() - mPrevX);
if (xDiff > mTouchSlop) {
return false;
}
}
return super.onInterceptTouchEvent(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment