覆寫兩個方法决摧,不再像個傻*一樣去判斷 MotionEvent
/**
* Created by kalshen on 2018/4/20.
* 傻*scrollView
* 垂直viewpager和scrollview的滾動沖突解決
*/
public class FoolishScrollView extends ScrollView {
private boolean isOverScroll = false;
public FoolishScrollView(Context context) {
super(context);
}
public FoolishScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
isOverScroll = clampedY;
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!isOverScroll) { //通知ViewPager不要干擾自身的操作
getParent().requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(ev);
} else {
getParent().requestDisallowInterceptTouchEvent(false);
return isOverScroll = false;//這里需要重置isOverScroll
}
}
}
順便再給你一個網(wǎng)絡修復版
public class MyScrollView extends ScrollView {
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private boolean mCanScroll = true;
private float mDownY;
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
int scrollY = getScrollY();
//滑到頂部或底部
if ((scrollY == 0 && mDownY <= ev.getY())
|| (getChildAt(0).getMeasuredHeight() == (scrollY + getHeight()) && mDownY >= ev.getY())) {
mCanScroll = false;
}
break;
case MotionEvent.ACTION_UP:
mCanScroll = true;
break;
case MotionEvent.ACTION_CANCEL:
mCanScroll = true;
break;
}
if (mCanScroll) { //通知ViewPager不要干擾自身的操作
getParent().requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(ev);
} else {
getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
}
}
兩個隨便選一個用就可以了。