Skip to content

Commit

Permalink
触摸事件优化,代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Mar 26, 2021
1 parent f33f790 commit 18e323b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public float getInterpolation(float t) {
*/
private boolean isTouchNotTriggerScrollStick = false;

/**
* 在快速滑动的过程中,触摸停止滑动
*/
private boolean isBrake = false;

public ConsecutiveScrollerLayout(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -449,6 +454,9 @@ public boolean dispatchTouchEvent(MotionEvent ev) {

switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:

isBrake = mScrollState == SCROLL_STATE_SETTLING;

// 停止滑动
stopScroll();
checkTargetsScroll(false, false);
Expand Down Expand Up @@ -529,14 +537,6 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
mDownLocation[0] = ScrollUtils.getRawX(this, ev, newPointerIndex);
mDownLocation[1] = ScrollUtils.getRawY(this, ev, newPointerIndex);
isTouchNotTriggerScrollStick = ScrollUtils.isTouchNotTriggerScrollStick(this, mDownLocation[0], mDownLocation[1]);

if (mAdjustVelocityTracker != null) {
mAdjustVelocityTracker.clear();
}

if (mAdjustVelocityTracker != null) {
mVelocityTracker.clear();
}
}
initAdjustVelocityTrackerIfNotExists();
mAdjustVelocityTracker.addMovement(vtev);
Expand Down Expand Up @@ -570,7 +570,6 @@ && isIntercept(ev) && Math.abs(yVelocity) >= mMinimumVelocity) {
mEventY = 0;
mEventX = 0;
mTouching = false;
SCROLL_ORIENTATION = SCROLL_NONE;
mDownLocation[0] = 0;
mDownLocation[1] = 0;
isTouchNotTriggerScrollStick = false;
Expand All @@ -584,6 +583,7 @@ && isIntercept(ev) && Math.abs(yVelocity) >= mMinimumVelocity) {
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
SCROLL_ORIENTATION = SCROLL_NONE;
recycleVelocityTracker();

if (mScroller.isFinished()) {
Expand Down Expand Up @@ -614,6 +614,10 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
stopNestedScroll(ViewCompat.TYPE_TOUCH);

if (isBrake && SCROLL_ORIENTATION == SCROLL_NONE){
return true;
}
break;
}
return super.onInterceptTouchEvent(ev);
Expand Down Expand Up @@ -1212,6 +1216,9 @@ private void checkTargetsScroll(boolean isLayoutChange, boolean isForce) {

for (int i = 0; i < index; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE){
continue;
}
if (ScrollUtils.isConsecutiveScrollerChild(child)) {
if (child instanceof IConsecutiveScroller) {
List<View> views = ((IConsecutiveScroller) child).getScrolledViews();
Expand All @@ -1230,6 +1237,9 @@ private void checkTargetsScroll(boolean isLayoutChange, boolean isForce) {

for (int i = index + 1; i < getChildCount(); i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE){
continue;
}
if (ScrollUtils.isConsecutiveScrollerChild(child)) {
if (i == getChildCount() - 1 && child.getHeight() < this.getHeight() && getScrollY() >= mScrollRange) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ static boolean canScrollVertically(View view) {
*/
static boolean canScrollVertically(View view, int direction) {
View scrolledView = getScrolledView(view);

if (scrolledView.getVisibility() == View.GONE){
// 隐藏状态,不能滑动
return false;
}

if (scrolledView instanceof AbsListView) {
AbsListView listView = (AbsListView) scrolledView;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Expand All @@ -137,7 +143,7 @@ static boolean canScrollVertically(View view, int direction) {

if (recyclerView.canScrollHorizontally(1) || recyclerView.canScrollVertically(-1)){
// 如果recyclerView可以水平滑动,并且使用canScrollVertically判断不能垂直滑动,这认定是不能垂直滑动的。
// 这样做既兼顾了recyclerView了同时水平、垂直滑动的情况,有保证了垂直滑动的判断是通过自定义的方式判断的
// 这样做既兼顾了recyclerView同时水平、垂直滑动的情况,又保证了垂直滑动的判断是通过自定义的方式判断的
if (!recyclerView.canScrollVertically(direction)){
return false;
}
Expand Down Expand Up @@ -435,7 +441,8 @@ static View getTopViewInTouch(ConsecutiveScrollerLayout csl, int touchX, int tou
}

if (ViewCompat.getZ(child) > ViewCompat.getZ(topTouchView) // 判断View的Z高度
|| csl.getDrawingPosition(child) > csl.getDrawingPosition(topTouchView)) { // 判断绘制顺序
|| (ViewCompat.getZ(child) == ViewCompat.getZ(topTouchView)
&& csl.getDrawingPosition(child) > csl.getDrawingPosition(topTouchView))) { // 判断绘制顺序
topTouchView = child;
}
}
Expand Down

0 comments on commit 18e323b

Please sign in to comment.