Skip to content

Commit 60300cf

Browse files
author
yangyufei
committed
add dampen
1 parent 0444ab8 commit 60300cf

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
.DS_Store
66
/build
77
.externalNativeBuild
8+
.idea/
89

library/src/main/java/jarvis/com/library/NestedTouchScrollingLayout.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ public class NestedTouchScrollingLayout extends FrameLayout implements NestedScr
7373
*/
7474
private @SheetDirection int mSheetDirection = SheetDirection.ALL;
7575

76+
/**
77+
* 手指向上阻尼值
78+
*/
79+
private float mDampingUp = 1;
80+
81+
/**
82+
* 手指向下阻尼值
83+
*/
84+
private float mDampingDown = 1;
85+
7686
/**
7787
************* 键盘收起,导致 reLayout,getHeight 发生改变,所以一开始就锁定高度
7888
*/
@@ -226,6 +236,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
226236

227237
@Override
228238
public boolean onTouchEvent(MotionEvent event) {
239+
float velocityY = 0;
229240
if (getChildAt(0) == null || !isParentDispatchTouchEvent) {
230241
return super.onTouchEvent(event);
231242
}
@@ -241,7 +252,7 @@ public boolean onTouchEvent(MotionEvent event) {
241252
mParentOwnsTouch = false;
242253
mDownY = event.getY();
243254
mDownX = event.getX();
244-
mSheetTranslation = getMeasuredHeight() - mOriginTranslate;
255+
mSheetTranslation = mTouchParentViewOriginMeasureHeight - mOriginTranslate;
245256
mDownSheetTranslation = mSheetTranslation;
246257
velocityTracker.clear();
247258

@@ -255,13 +266,16 @@ public boolean onTouchEvent(MotionEvent event) {
255266
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
256267
isFingerHolderTouch = false;
257268
isLeftorRightTouchLimit = true;
269+
velocityTracker.computeCurrentVelocity(1000);
270+
velocityY = velocityTracker.getYVelocity();
271+
notifyOnFingerUp(velocityY);
258272
}
259273

260274
getParent().requestDisallowInterceptTouchEvent(true);
261275

262276
velocityTracker.addMovement(event);
263277

264-
float maxSheetTranslation = getMeasuredHeight();
278+
float maxSheetTranslation = mTouchParentViewOriginMeasureHeight;
265279

266280
float deltaY = mDownY - event.getY();
267281
float deltaX = mDownX - event.getX();
@@ -312,22 +326,25 @@ public boolean onTouchEvent(MotionEvent event) {
312326
}
313327

314328
if (isHoldTouch) {
315-
event.offsetLocation(0, mSheetTranslation - getMeasuredHeight());
329+
event.offsetLocation(0, mSheetTranslation - mTouchParentViewOriginMeasureHeight);
316330
getChildAt(0).dispatchTouchEvent(event);
317331
} else {
318332
setSheetTranslation(newSheetTranslation);
319333

320334
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
321335
isHoldTouch = true;
322336
getParent().requestDisallowInterceptTouchEvent(false);
323-
velocityTracker.computeCurrentVelocity(1000);
324-
float velocityY = velocityTracker.getYVelocity();
325-
// recover(0);
337+
338+
if (Math.abs(velocityY) < minFlingVelocity) {
339+
if (mSheetTranslation > getHeight() / 2) { } else { }
340+
} else {
341+
if (velocityY < 0) { } else { }
342+
}
326343
notifyNestScrollChildReleaseCallback((int) velocityY);
327344
}
328345
}
329346
} else {
330-
event.offsetLocation(0, mSheetTranslation - getMeasuredHeight());
347+
event.offsetLocation(0, mSheetTranslation - mTouchParentViewOriginMeasureHeight);
331348
getChildAt(0).dispatchTouchEvent(event);
332349
}
333350
return true;
@@ -475,12 +492,13 @@ public void setTranslation(float transY) {
475492
if (mSheetDirection == SheetDirection.TOP && transY > 0) {
476493
return;
477494
}
495+
transY = transY > 0 ? transY * mDampingDown : transY * mDampingUp;
478496
notifyNestScrollChildChangeCallback(transY);
479497
if (mChildView != null) {
480498
mChildView.setTranslationY(transY);
481499
}
482500
if (transY == 0) {
483-
mDownSheetTranslation = getMeasuredHeight();
501+
mDownSheetTranslation = mTouchParentViewOriginMeasureHeight;
484502
mOriginTranslate = 0;
485503
}
486504
}
@@ -694,4 +712,12 @@ public void peek(int offset, Runnable runnable) {
694712
public void hiden(Runnable runnable) {
695713
recover(getMeasuredHeight(), runnable);
696714
}
715+
716+
public void setDampingDown(float mDampingDown) {
717+
this.mDampingDown = mDampingDown;
718+
}
719+
720+
public void setDampingUp(float mDampingUp) {
721+
this.mDampingUp = mDampingUp;
722+
}
697723
}

0 commit comments

Comments
 (0)