Skip to content

Commit 3a3b2ac

Browse files
committed
Fix bugs of OverScroll when TargetView scrolls at the top/bottom.
1 parent 42ecfe2 commit 3a3b2ac

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

library/src/main/java/com/lcodecore/tkrefreshlayout/TwinklingRefreshLayout.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,22 @@ protected void onFinishInflate() {
176176

177177
private void initGestureDetector() {
178178
gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
179+
@Override
180+
public boolean onDown(MotionEvent ev) {
181+
decorator.onFingerDown(ev);
182+
return false;
183+
}
179184

180185
@Override
181186
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
182187
decorator.onFingerScroll(e1, e2, distanceX, distanceY, vy);
183-
return super.onScroll(e1, e2, distanceX, distanceY);
188+
return false;
184189
}
185190

186191
@Override
187192
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
188193
decorator.onFingerFling(e1, e2, velocityX, velocityY);
189-
return super.onFling(e1, e2, velocityX, velocityY);
194+
return false;
190195
}
191196
});
192197
}
@@ -517,6 +522,7 @@ public void setOverScrollHeight(float overScrollHeightDp) {
517522
*/
518523
public void setAutoLoadMore(boolean ifAutoLoadMore) {
519524
autoLoadMore = ifAutoLoadMore;
525+
setEnableLoadmore(true);
520526
}
521527

522528
/**

library/src/main/java/com/lcodecore/tkrefreshlayout/processor/AnimProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void animBottomBack() {
144144
animLayoutByTime(getVisibleFootHeight(), 0, new AnimatorUpdateListener() {
145145
@Override
146146
public void onAnimationUpdate(ValueAnimator animation) {
147-
if (!ScrollingUtil.isViewTopBottom(cp.getTargetView(),cp.getTouchSlop())){
147+
if (!ScrollingUtil.isViewToBottom(cp.getTargetView(),cp.getTouchSlop())){
148148
int dy = getVisibleFootHeight() - (int) animation.getAnimatedValue();
149149
//可以让TargetView滚动dy高度,但这样两个方向上滚动感觉画面闪烁,改为dy/2是为了消除闪烁
150150
if (dy > 0) {

library/src/main/java/com/lcodecore/tkrefreshlayout/processor/IDecorator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public interface IDecorator {
1111

1212
boolean dealTouchEvent(MotionEvent ev);
1313

14-
boolean onFingerScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY, float velocityY);
14+
void onFingerDown(MotionEvent ev);
1515

16-
boolean onFingerFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
16+
void onFingerScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY, float velocityY);
17+
18+
void onFingerFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
1719
}

library/src/main/java/com/lcodecore/tkrefreshlayout/processor/OverScrollDecorator.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,47 @@ public OverScrollDecorator(TwinklingRefreshLayout.CoContext processor, IDecorato
3535

3636
@Override
3737
public boolean interceptTouchEvent(MotionEvent ev) {
38-
return decorator!=null && decorator.interceptTouchEvent(ev);
38+
return decorator != null && decorator.interceptTouchEvent(ev);
3939
}
4040

4141
@Override
4242
public boolean dealTouchEvent(MotionEvent e) {
43-
return decorator!=null && decorator.dealTouchEvent(e);
43+
return decorator != null && decorator.dealTouchEvent(e);
4444
}
4545

46+
private boolean preventTopOverScroll = false;
47+
private boolean preventBottomOverScroll = false;
48+
4649
@Override
47-
public boolean onFingerScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY, float velocityY) {
48-
return decorator!=null && decorator.onFingerScroll(e1,e2,distanceX,distanceY,velocityY);
50+
public void onFingerDown(MotionEvent ev) {
51+
if (decorator != null) decorator.onFingerDown(ev);
52+
53+
preventTopOverScroll = ScrollingUtil.isViewToTop(cp.getTargetView(), cp.getTouchSlop());
54+
preventBottomOverScroll = ScrollingUtil.isViewToBottom(cp.getTargetView(), cp.getTouchSlop());
4955
}
5056

5157
@Override
52-
public boolean onFingerFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
53-
if (decorator!=null) decorator.onFingerFling(e1,e2,velocityX,velocityY);
58+
public void onFingerScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY, float velocityY) {
59+
if (decorator != null) decorator.onFingerScroll(e1, e2, distanceX, distanceY, velocityY);
60+
}
61+
62+
@Override
63+
public void onFingerFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
64+
if (decorator != null) decorator.onFingerFling(e1, e2, velocityX, velocityY);
5465
//fling时才触发OverScroll,获取速度并采用演示策略估算View是否滚动到边界
55-
if (!cp.enableOverScroll()) return false;
66+
if (!cp.enableOverScroll()) return;
67+
68+
int dy = (int) (e2.getY() - e1.getY());
69+
if (dy < -cp.getTouchSlop() && preventBottomOverScroll) return;//控件滚动在底部且向上fling
70+
if (dy > cp.getTouchSlop() && preventTopOverScroll) return;//控件滚动在顶部且向下fling
71+
5672
mVelocityY = velocityY;
5773
if (Math.abs(mVelocityY) >= OVER_SCROLL_MIN_VX) {
5874
mHandler.sendEmptyMessage(MSG_START_COMPUTE_SCROLL);
5975
} else {
6076
mVelocityY = 0;
6177
cur_delay_times = ALL_DELAY_TIMES;
6278
}
63-
return false;
6479
}
6580

6681
private Handler mHandler = new Handler() {
@@ -84,7 +99,7 @@ public void handleMessage(Message msg) {
8499
cur_delay_times = ALL_DELAY_TIMES;
85100
}
86101
} else if (mVelocityY <= -OVER_SCROLL_MIN_VX) {
87-
if (ScrollingUtil.isViewTopBottom(mChildView, mTouchSlop)) {
102+
if (ScrollingUtil.isViewToBottom(mChildView, mTouchSlop)) {
88103
cp.getAnimProcessor().animOverScrollBottom(mVelocityY, cur_delay_times);
89104
mVelocityY = 0;
90105
cur_delay_times = ALL_DELAY_TIMES;

library/src/main/java/com/lcodecore/tkrefreshlayout/processor/RefreshProcessor.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public boolean interceptTouchEvent(MotionEvent ev) {
3131
float dx = ev.getX() - mTouchX;
3232
float dy = ev.getY() - mTouchY;
3333
if (Math.abs(dx) <= Math.abs(dy)) {//滑动允许最大角度为45度
34-
if (dy > 0 && !ScrollingUtil.canChildScrollUp(cp.getTargetView()) && cp.allowPullDown()) {
34+
if (dy > 0 && ScrollingUtil.isViewToTop(cp.getTargetView(),cp.getTouchSlop()) && cp.allowPullDown()) {
3535
cp.setStatePTD();
3636
return true;
37-
} else if (dy < 0 && !ScrollingUtil.canChildScrollDown(cp.getTargetView()) && cp.allowPullUp()) {
37+
} else if (dy < 0 && ScrollingUtil.isViewToBottom(cp.getTargetView(),cp.getTouchSlop()) && cp.allowPullUp()) {
3838
cp.setStatePBU();
3939
return true;
4040
}
@@ -75,7 +75,11 @@ public boolean dealTouchEvent(MotionEvent e) {
7575
}
7676

7777
@Override
78-
public boolean onFingerScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY, float velocityY) {
78+
public void onFingerDown(MotionEvent ev) {
79+
}
80+
81+
@Override
82+
public void onFingerScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY, float velocityY) {
7983
//手指在屏幕上滚动,如果此时正处在刷新状态,可隐藏
8084
int mTouchSlop = cp.getTouchSlop();
8185
if (cp.isRefreshVisible() && distanceY >= mTouchSlop && !cp.isOpenFloatRefresh()) {
@@ -86,11 +90,9 @@ public boolean onFingerScroll(MotionEvent e1, MotionEvent e2, float distanceX, f
8690
cp.setLoadingMore(false);
8791
cp.getAnimProcessor().animBottomHideByVy((int) velocityY);
8892
}
89-
return false;
9093
}
9194

9295
@Override
93-
public boolean onFingerFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
94-
return false;
96+
public void onFingerFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
9597
}
9698
}

library/src/main/java/com/lcodecore/tkrefreshlayout/utils/ScrollingUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ public static boolean isScrollViewOrWebViewToTop(View view) {
8989
public static boolean isViewToTop(View view,int mTouchSlop){
9090
if (view instanceof AbsListView) return isAbsListViewToTop((AbsListView) view);
9191
if (view instanceof RecyclerView) return isRecyclerViewToTop((RecyclerView) view);
92-
System.out.println("View的scrollY:"+view.getScrollY());
9392
return (view != null && Math.abs(view.getScrollY()) <= 2 * mTouchSlop);
9493
}
9594

96-
public static boolean isViewTopBottom(View view,int mTouchSlop){
95+
public static boolean isViewToBottom(View view,int mTouchSlop){
9796
if (view instanceof AbsListView) return isAbsListViewToBottom((AbsListView) view);
9897
if (view instanceof RecyclerView) return isRecyclerViewToBottom((RecyclerView) view);
9998
if (view instanceof WebView) return isWebViewToBottom((WebView) view,mTouchSlop);

0 commit comments

Comments
 (0)