Skip to content

Commit 444d5d3

Browse files
author
Jens Claes
committed
Made horizontal scrolling more intuitive
When a scroll is started, the user probably wants to finish it. This makes scrolls a lot more intuitive (requires way less work from the user's thumb), especially when noOfVisibleDays equals 1.
1 parent f753839 commit 444d5d3

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

library/src/main/java/com/alamkanak/weekview/WeekView.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ public boolean onDown(MotionEvent e) {
114114
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
115115
if (mCurrentScrollDirection == Direction.NONE) {
116116
if (Math.abs(distanceX) > Math.abs(distanceY)){
117-
mCurrentScrollDirection = Direction.HORIZONTAL;
118-
mCurrentFlingDirection = Direction.HORIZONTAL;
117+
if(distanceX > 0)
118+
mCurrentScrollDirection = Direction.LEFT;
119+
else
120+
mCurrentScrollDirection = Direction.RIGHT;
121+
122+
mCurrentFlingDirection = mCurrentScrollDirection;
119123
}
120124
else {
121125
mCurrentFlingDirection = Direction.VERTICAL;
@@ -133,7 +137,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
133137
mScroller.forceFinished(true);
134138
mStickyScroller.forceFinished(true);
135139

136-
if (mCurrentFlingDirection == Direction.HORIZONTAL){
140+
if (mCurrentFlingDirection.isHorizontal()){
137141
mScroller.fling((int) mCurrentOrigin.x, 0, (int) velocityX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
138142
}
139143
else if (mCurrentFlingDirection == Direction.VERTICAL){
@@ -181,7 +185,16 @@ public void onLongPress(MotionEvent e) {
181185

182186

183187
private enum Direction {
184-
NONE, HORIZONTAL, VERTICAL
188+
NONE(false), VERTICAL(false), LEFT(true), RIGHT(true);
189+
190+
public boolean isHorizontal(){
191+
return this.isHorizontal;
192+
}
193+
194+
Direction(boolean isHorizontal){
195+
this.isHorizontal = isHorizontal;
196+
}
197+
private boolean isHorizontal;
185198
}
186199

187200
public WeekView(Context context) {
@@ -357,7 +370,7 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
357370
}
358371

359372
// Consider scroll offset.
360-
if (mCurrentScrollDirection == Direction.HORIZONTAL) mCurrentOrigin.x -= mDistanceX;
373+
if (mCurrentScrollDirection.isHorizontal()) mCurrentOrigin.x -= mDistanceX;
361374
int leftDaysWithGaps = (int) -(Math.ceil(mCurrentOrigin.x / (mWidthPerDay + mColumnGap)));
362375
float startFromPixel = mCurrentOrigin.x + (mWidthPerDay + mColumnGap) * leftDaysWithGaps +
363376
mHeaderColumnWidth;
@@ -1133,27 +1146,28 @@ public Calendar getLastVisibleDay() {
11331146
@Override
11341147
public boolean onTouchEvent(MotionEvent event) {
11351148
if (event.getAction() == MotionEvent.ACTION_UP) {
1136-
1137-
if (mCurrentScrollDirection == Direction.HORIZONTAL) {
1149+
if (mCurrentScrollDirection.isHorizontal()) {
11381150
float leftDays = Math.round(mCurrentOrigin.x / (mWidthPerDay + mColumnGap));
11391151
int nearestOrigin = (int) (mCurrentOrigin.x - leftDays * (mWidthPerDay+mColumnGap));
11401152
mStickyScroller.startScroll((int) mCurrentOrigin.x, 0, - nearestOrigin, 0);
11411153
ViewCompat.postInvalidateOnAnimation(WeekView.this);
11421154
}
1143-
mCurrentScrollDirection = Direction.NONE;
11441155
}
11451156
return mGestureDetector.onTouchEvent(event);
11461157
}
11471158

1148-
11491159
@Override
11501160
public void computeScroll() {
11511161
super.computeScroll();
11521162
if (mScroller.computeScrollOffset()) {
11531163
if (Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) < mWidthPerDay + mColumnGap && Math.abs(mScroller.getFinalX() - mScroller.getStartX()) != 0) {
11541164
mScroller.forceFinished(true);
11551165
float leftDays = Math.round(mCurrentOrigin.x / (mWidthPerDay + mColumnGap));
1156-
int nearestOrigin = (int) (mCurrentOrigin.x - leftDays * (mWidthPerDay+mColumnGap));
1166+
if(mCurrentScrollDirection == Direction.LEFT)
1167+
leftDays--;
1168+
else
1169+
leftDays++;
1170+
int nearestOrigin = (int) ((mCurrentOrigin.x - leftDays * (mWidthPerDay+mColumnGap)));
11571171
mStickyScroller.startScroll((int) mCurrentOrigin.x, 0, - nearestOrigin, 0);
11581172
ViewCompat.postInvalidateOnAnimation(WeekView.this);
11591173
}
@@ -1162,6 +1176,7 @@ public void computeScroll() {
11621176
else mCurrentOrigin.x = mScroller.getCurrX();
11631177
ViewCompat.postInvalidateOnAnimation(this);
11641178
}
1179+
mCurrentScrollDirection = Direction.NONE;
11651180
}
11661181
if (mStickyScroller.computeScrollOffset()) {
11671182
mCurrentOrigin.x = mStickyScroller.getCurrX();

0 commit comments

Comments
 (0)