Skip to content

Commit

Permalink
Update CalendarLayout.java
Browse files Browse the repository at this point in the history
滑动事件过于灵敏问题处理
  • Loading branch information
codingbooo committed Aug 12, 2019
1 parent 8fb4654 commit 1f87b84
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public class CalendarLayout extends LinearLayout {

private float downY;
private float mLastY;
private float mLastX;
private boolean isAnimating = false;

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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已经收缩,不拦截事件
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 1f87b84

Please sign in to comment.