diff --git a/calendarview/src/main/java/com/haibin/calendarview/CalendarLayout.java b/calendarview/src/main/java/com/haibin/calendarview/CalendarLayout.java index 83158242..2a5c8e14 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/CalendarLayout.java +++ b/calendarview/src/main/java/com/haibin/calendarview/CalendarLayout.java @@ -147,6 +147,7 @@ public class CalendarLayout extends LinearLayout { private float downY; private float mLastY; + private float mLastX; private boolean isAnimating = false; /** @@ -296,7 +297,7 @@ public boolean onTouchEvent(MotionEvent event) { mCalendarShowMode == CALENDAR_SHOW_MODE_ONLY_WEEK_VIEW) {//禁用手势,或者只显示某种视图 return false; } - if(mDelegate == null){ + if (mDelegate == null) { return false; } if (mDelegate.isShowYearSelectedLayout) { @@ -318,10 +319,10 @@ public boolean onTouchEvent(MotionEvent event) { return true; case MotionEvent.ACTION_POINTER_DOWN: { final int indexx = event.getActionIndex(); - mActivePointerId = event.getPointerId( indexx); + mActivePointerId = event.getPointerId(indexx); if (mActivePointerId == 0) { //核心代码:就是让下面的 dy = y- mLastY == 0,避免抖动 - mLastY = event.getY( mActivePointerId); + mLastY = event.getY(mActivePointerId); } break; } @@ -473,14 +474,17 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { } final int action = ev.getAction(); float y = ev.getY(); + float x = ev.getX(); switch (action) { case MotionEvent.ACTION_DOWN: int index = ev.getActionIndex(); mActivePointerId = ev.getPointerId(index); mLastY = downY = y; + mLastX = x; break; case MotionEvent.ACTION_MOVE: float dy = y - mLastY; + float dx = x - mLastX; /* 如果向上滚动,且ViewPager已经收缩,不拦截事件 */ @@ -503,7 +507,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { return false; } - if (Math.abs(dy) > mTouchSlop) {//大于mTouchSlop开始拦截事件,ContentView和ViewPager得到CANCEL事件 + if (Math.abs(dy) > Math.abs(dx) ) { //纵向滑动距离大于横向滑动距离,拦截滑动事件 if ((dy > 0 && mContentView.getTranslationY() <= 0) || (dy < 0 && mContentView.getTranslationY() >= -mContentViewTranslateY)) { mLastY = y; @@ -839,7 +843,7 @@ private void hideWeek(boolean isNotify) { */ private void showWeek() { onShowWeekView(); - if(mWeekPager != null && mWeekPager.getAdapter()!= null){ + if (mWeekPager != null && mWeekPager.getAdapter() != null) { mWeekPager.getAdapter().notifyDataSetChanged(); mWeekPager.setVisibility(VISIBLE); }