Skip to content

Commit b3ec59f

Browse files
committed
continuous drag & click optimization
1 parent 00d55a2 commit b3ec59f

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

library/src/main/java/com/stone/pile/libs/PileLayout.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
228228
case MotionEvent.ACTION_CANCEL:
229229
case MotionEvent.ACTION_UP:
230230
recycleVelocityTracker();
231+
// ACTION_UP还能拦截,说明手指没滑动,只是一个click事件,同样需要snap到特定位置
232+
onRelease(event.getX(), 0);
231233
break;
232234
}
233235
return false; // 默认都是不拦截的
@@ -256,32 +258,35 @@ public boolean onTouchEvent(MotionEvent event) {
256258
int velocity = (int) velocityTracker.getXVelocity();
257259
recycleVelocityTracker();
258260

259-
animatingView = (FrameLayout) getChildAt(3);
260-
animateValue = animatingView.getLeft();
261-
int tag = Integer.parseInt(animatingView.getTag().toString());
262-
263-
// 计算目标位置
264-
int destX = originX.get(3);
265-
if (velocity > VELOCITY_THRESHOLD || (animatingView.getLeft() > originX.get(3) + scrollDistanceMax / 2 && velocity > -VELOCITY_THRESHOLD)) {
266-
destX = originX.get(4);
267-
tag--;
268-
}
269-
if (tag < 0 || tag >= adapter.getItemCount()) {
270-
return true;
271-
}
261+
onRelease(event.getX(), velocity);
262+
break;
263+
}
264+
return true;
265+
}
272266

273-
if (Math.abs(animatingView.getLeft() - destX) < mTouchSlop && Math.abs(event.getX() - downX) < mTouchSlop) {
274-
return true;
275-
}
267+
private void onRelease(float eventX, int velocityX) {
268+
animatingView = (FrameLayout) getChildAt(3);
269+
animateValue = animatingView.getLeft();
270+
int tag = Integer.parseInt(animatingView.getTag().toString());
276271

277-
adapter.displaying(tag);
278-
animator = ObjectAnimator.ofFloat(this, "animateValue", animatingView.getLeft(), destX);
279-
animator.setInterpolator(interpolator);
280-
animator.setDuration(360).start();
272+
// 计算目标位置
273+
int destX = originX.get(3);
274+
if (velocityX > VELOCITY_THRESHOLD || (animatingView.getLeft() > originX.get(3) + scrollDistanceMax / 2 && velocityX > -VELOCITY_THRESHOLD)) {
275+
destX = originX.get(4);
276+
tag--;
277+
}
278+
if (tag < 0 || tag >= adapter.getItemCount()) {
279+
return;
280+
}
281281

282-
break;
282+
if (Math.abs(animatingView.getLeft() - destX) < mTouchSlop && Math.abs(eventX - downX) < mTouchSlop) {
283+
return;
283284
}
284-
return true;
285+
286+
adapter.displaying(tag);
287+
animator = ObjectAnimator.ofFloat(this, "animateValue", animatingView.getLeft(), destX);
288+
animator.setInterpolator(interpolator);
289+
animator.setDuration(360).start();
285290
}
286291

287292
private void requireScrollChange(int dx) {

0 commit comments

Comments
 (0)