Skip to content

Commit 6aac43d

Browse files
author
teach
committed
改bug;代码优化
1 parent ecd9d33 commit 6aac43d

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,10 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
672672
int touchY = ScrollUtils.getRawY(this, ev, actionIndex);
673673
View targetView = getTouchTarget(touchX, touchY);
674674
boolean canScrollVerticallyChild = ScrollUtils.canScrollVertically(targetView);
675-
boolean canScrollHorizontallyChild = ScrollUtils.canScrollHorizontally(targetView);
675+
boolean canScrollHorizontallyChild = ScrollUtils.isHorizontalScroll(this, touchX, touchY);
676676
if (SCROLL_ORIENTATION != SCROLL_VERTICAL && canScrollVerticallyChild
677677
&& Math.abs(yVelocity) >= mMinimumVelocity
678-
&& !ScrollUtils.isHorizontalScroll(this, touchX, touchY)) {
678+
&& !canScrollHorizontallyChild) {
679679
//如果当前是横向滑动,但是触摸的控件可以垂直滑动,并且产生垂直滑动的fling事件,
680680
// 为了不让这个控件垂直fling,把事件设置为MotionEvent.ACTION_CANCEL。
681681
ev.setAction(MotionEvent.ACTION_CANCEL);

consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ScrollUtils.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
*/
2323
public class ScrollUtils {
2424

25+
static Method computeVerticalScrollOffsetMethod;
26+
static Method computeVerticalScrollRangeMethod;
27+
static Method computeVerticalScrollExtentMethod;
28+
2529
static int computeVerticalScrollOffset(View view) {
2630
View scrolledView = getScrolledView(view);
2731

@@ -30,9 +34,11 @@ static int computeVerticalScrollOffset(View view) {
3034
}
3135

3236
try {
33-
Method method = View.class.getDeclaredMethod("computeVerticalScrollOffset");
34-
method.setAccessible(true);
35-
Object o = method.invoke(scrolledView);
37+
if (computeVerticalScrollOffsetMethod == null){
38+
computeVerticalScrollOffsetMethod = View.class.getDeclaredMethod("computeVerticalScrollOffset");
39+
computeVerticalScrollOffsetMethod.setAccessible(true);
40+
}
41+
Object o = computeVerticalScrollOffsetMethod.invoke(scrolledView);
3642
if (o != null){
3743
return (int) o;
3844
}
@@ -50,9 +56,11 @@ static int computeVerticalScrollRange(View view) {
5056
}
5157

5258
try {
53-
Method method = View.class.getDeclaredMethod("computeVerticalScrollRange");
54-
method.setAccessible(true);
55-
Object o = method.invoke(scrolledView);
59+
if (computeVerticalScrollRangeMethod == null) {
60+
computeVerticalScrollRangeMethod = View.class.getDeclaredMethod("computeVerticalScrollRange");
61+
computeVerticalScrollRangeMethod.setAccessible(true);
62+
}
63+
Object o = computeVerticalScrollRangeMethod.invoke(scrolledView);
5664
if (o != null){
5765
return (int) o;
5866
}
@@ -70,9 +78,12 @@ static int computeVerticalScrollExtent(View view) {
7078
}
7179

7280
try {
73-
Method method = View.class.getDeclaredMethod("computeVerticalScrollExtent");
74-
method.setAccessible(true);
75-
Object o = method.invoke(scrolledView);
81+
if (computeVerticalScrollExtentMethod == null){
82+
computeVerticalScrollExtentMethod = View.class.getDeclaredMethod("computeVerticalScrollExtent");
83+
computeVerticalScrollExtentMethod.setAccessible(true);
84+
}
85+
86+
Object o = computeVerticalScrollExtentMethod.invoke(scrolledView);
7687
if (o != null){
7788
return (int) o;
7889
}

0 commit comments

Comments
 (0)