Last active
July 18, 2016 21:28
-
-
Save TakWolf/d9d59a7817858fc404b95894356c06b9 to your computer and use it in GitHub Desktop.
修复RefreshLayout和ViewPager手势冲突
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 修复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