@@ -228,6 +228,8 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
228
228
case MotionEvent .ACTION_CANCEL :
229
229
case MotionEvent .ACTION_UP :
230
230
recycleVelocityTracker ();
231
+ // ACTION_UP还能拦截,说明手指没滑动,只是一个click事件,同样需要snap到特定位置
232
+ onRelease (event .getX (), 0 );
231
233
break ;
232
234
}
233
235
return false ; // 默认都是不拦截的
@@ -256,32 +258,35 @@ public boolean onTouchEvent(MotionEvent event) {
256
258
int velocity = (int ) velocityTracker .getXVelocity ();
257
259
recycleVelocityTracker ();
258
260
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
+ }
272
266
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 ());
276
271
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
+ }
281
281
282
- break ;
282
+ if (Math .abs (animatingView .getLeft () - destX ) < mTouchSlop && Math .abs (eventX - downX ) < mTouchSlop ) {
283
+ return ;
283
284
}
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 ();
285
290
}
286
291
287
292
private void requireScrollChange (int dx ) {
0 commit comments