SwipeRefreshLayout在向下滑动时禁用拖动animation

是否可以禁用SwipeRefreshLayout拖动animation不需要类定制?

尝试拨打:

 setEnabled(false) 

在您的SwipeRefreshLayout视图上。

那么,禁用SwipeLayoutAnimanion似乎是一个相当简单的任务,但它涉及到一个项目中android.support.v4.widget.SwipeRefreshLayout类的复制。

在源代码中潜水会发现, SwipeRefreshLayout由三个类组成:

  • android.support.v4.widget.SwipeRefreshLayout
  • android.support.v4.widget.SwipeProgressBar
  • android.support.v4.widget.BakedBezierInterpolator

这三个class级都应该包含在项目中。 然后SwipeRefreshLayout可以自定义如下:

添加一个新的公共方法,将控制布局应该遵循向下滑动的手势:

 private boolean mLayoutMovementEnabled = true; public void setLayoutMovementEnabled(boolean enabled) { mLayoutMovementEnabled = enabled; } 

所有相关的计算都在onTouchEvent()内部执行。 要在移动后禁用布局,

updateContentOffsetTop((int) (offsetTop)); string应该改为

 if (mLayoutMovementEnabled) updateContentOffsetTop((int) (offsetTop)); 

完整的修改例程如下。

 @Override public boolean onTouchEvent(MotionEvent event) { final int action = event.getAction(); boolean handled = false; switch (action) { case MotionEvent.ACTION_DOWN: mCurrPercentage = 0; mDownEvent = MotionEvent.obtain(event); mPrevY = mDownEvent.getY(); break; case MotionEvent.ACTION_MOVE: if (mDownEvent != null && !mReturningToStart) { final float eventY = event.getY(); float yDiff = eventY - mDownEvent.getY(); if (yDiff > mTouchSlop) { // User velocity passed min velocity; trigger a refresh if (yDiff > mDistanceToTriggerSync) { // User movement passed distance; trigger a refresh startRefresh(); handled = true; break; } else { // Just track the user's movement setTriggerPercentage( mAccelerateInterpolator.getInterpolation( yDiff / mDistanceToTriggerSync)); float offsetTop = yDiff; if (mPrevY > eventY) { offsetTop = yDiff - mTouchSlop; } if (mLayoutMovementEnabled) updateContentOffsetTop((int) (offsetTop)); if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) { // If the user puts the view back at the top, we // don't need to. This shouldn't be considered // cancelling the gesture as the user can restart from the top. removeCallbacks(mCancel); } else { updatePositionTimeout(); } mPrevY = event.getY(); handled = true; } } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (mDownEvent != null) { mDownEvent.recycle(); mDownEvent = null; } break; } return handled; } 

一个简单的方法来禁用滑动是设置的距离触发同步到一些价值太高,无法达成。

 mSwipeLayout.setDistanceToTriggerSync(999999);