向下滑动时,SwipeRefreshLayout禁用拖动动画

时间:2014-09-28 19:41:33

标签: android swiperefreshlayout

是否可以在不进行类自定义的情况下在向下滑动时禁用SwipeRefreshLayout拖动动画?

3 个答案:

答案 0 :(得分:70)

尝试致电:

setEnabled(false)
在您的SwipeRefreshLayout视图上

答案 1 :(得分:12)

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

源代码潜水会显示,SwipeRefreshLayout由三个类组成:

所有三个类都应该包含在项目中。然后可以按如下方式自定义SwipeRefreshLayout

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

private boolean mLayoutMovementEnabled = true;

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

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

updateContentOffsetTop((int) (offsetTop));字符串应更改为

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;
}

答案 2 :(得分:5)

禁用滑动的一种简单方法是将触发同步的距离设置为某个值太高而无法达到。

mSwipeLayout.setDistanceToTriggerSync(999999);