Android SwipeRefreshLayout 禁用向下滑动时的拖动动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26088878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:59:24  来源:igfitidea点击:

SwipeRefreshLayout disable drag animation on swipe down

androidswiperefreshlayout

提问by Alexander Zhak

Is it possible to disable SwipeRefreshLayoutdrag animation on swipe down without class customization?

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

回答by carloshwa

Try calling:

尝试调用:

setEnabled(false)

on your SwipeRefreshLayout view.

在您的 SwipeRefreshLayout 视图上。

回答by Alexander Zhak

Well, disabling SwipeLayoutAnimanion appeared to be a rather simple task, but it involves replication of android.support.v4.widget.SwipeRefreshLayoutclass inside one's project.

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

Diving in source code will reveal, that SwipeRefreshLayoutconsists of three classes:

深入研究源代码会发现,它SwipeRefreshLayout包含三个类:

All three classes should be included in the project. Then SwipeRefreshLayoutcan be customized as follows:

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

Add a new public method which will control either layout should follow the swipe down gesture or not:

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

private boolean mLayoutMovementEnabled = true;

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

All related computations are performed inside onTouchEvent(). To disable layout following the movement,

所有相关的计算都在onTouchEvent(). 要在移动后禁用布局,

updateContentOffsetTop((int) (offsetTop));string should be changed to

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

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

The complete modified routine is below.

完整的修改例程如下。

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

回答by Sylvain Pennec

One simple way to disable the swipe is to set the distance to trigger sync to some value too high to be reached.

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

mSwipeLayout.setDistanceToTriggerSync(999999);