Skip to content

Commit

Permalink
改bug;代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Dec 22, 2021
1 parent ecd9d33 commit 6aac43d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,10 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
int touchY = ScrollUtils.getRawY(this, ev, actionIndex);
View targetView = getTouchTarget(touchX, touchY);
boolean canScrollVerticallyChild = ScrollUtils.canScrollVertically(targetView);
boolean canScrollHorizontallyChild = ScrollUtils.canScrollHorizontally(targetView);
boolean canScrollHorizontallyChild = ScrollUtils.isHorizontalScroll(this, touchX, touchY);
if (SCROLL_ORIENTATION != SCROLL_VERTICAL && canScrollVerticallyChild
&& Math.abs(yVelocity) >= mMinimumVelocity
&& !ScrollUtils.isHorizontalScroll(this, touchX, touchY)) {
&& !canScrollHorizontallyChild) {
//如果当前是横向滑动,但是触摸的控件可以垂直滑动,并且产生垂直滑动的fling事件,
// 为了不让这个控件垂直fling,把事件设置为MotionEvent.ACTION_CANCEL。
ev.setAction(MotionEvent.ACTION_CANCEL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
*/
public class ScrollUtils {

static Method computeVerticalScrollOffsetMethod;
static Method computeVerticalScrollRangeMethod;
static Method computeVerticalScrollExtentMethod;

static int computeVerticalScrollOffset(View view) {
View scrolledView = getScrolledView(view);

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

try {
Method method = View.class.getDeclaredMethod("computeVerticalScrollOffset");
method.setAccessible(true);
Object o = method.invoke(scrolledView);
if (computeVerticalScrollOffsetMethod == null){
computeVerticalScrollOffsetMethod = View.class.getDeclaredMethod("computeVerticalScrollOffset");
computeVerticalScrollOffsetMethod.setAccessible(true);
}
Object o = computeVerticalScrollOffsetMethod.invoke(scrolledView);
if (o != null){
return (int) o;
}
Expand All @@ -50,9 +56,11 @@ static int computeVerticalScrollRange(View view) {
}

try {
Method method = View.class.getDeclaredMethod("computeVerticalScrollRange");
method.setAccessible(true);
Object o = method.invoke(scrolledView);
if (computeVerticalScrollRangeMethod == null) {
computeVerticalScrollRangeMethod = View.class.getDeclaredMethod("computeVerticalScrollRange");
computeVerticalScrollRangeMethod.setAccessible(true);
}
Object o = computeVerticalScrollRangeMethod.invoke(scrolledView);
if (o != null){
return (int) o;
}
Expand All @@ -70,9 +78,12 @@ static int computeVerticalScrollExtent(View view) {
}

try {
Method method = View.class.getDeclaredMethod("computeVerticalScrollExtent");
method.setAccessible(true);
Object o = method.invoke(scrolledView);
if (computeVerticalScrollExtentMethod == null){
computeVerticalScrollExtentMethod = View.class.getDeclaredMethod("computeVerticalScrollExtent");
computeVerticalScrollExtentMethod.setAccessible(true);
}

Object o = computeVerticalScrollExtentMethod.invoke(scrolledView);
if (o != null){
return (int) o;
}
Expand Down

0 comments on commit 6aac43d

Please sign in to comment.