Skip to content

使用AsyncLayoutInflater初始化SmartRefreshLayout导致无法刷新 #1129

Open
@LiveSalton

Description

详细描述
使用AsyncLayoutInflater对SmartRefreshLayout构造导致子线程执行,致使finishRefresh无法执行。

使用版本

implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.2' 

使用代码

@Override
    public RefreshLayout finishRefresh(final int delayed, final boolean success, final Boolean noMoreData) {
        final int more = delayed >> 16;
        int delay = delayed << 16 >> 16;
        Runnable runnable = new Runnable() {
            int count = 0;
            @Override
            public void run() {
                if (count == 0) {
                    if (mState == RefreshState.None && mViceState == RefreshState.Refreshing) {
                        //autoRefresh 即将执行,但未开始
                        mViceState = RefreshState.None;
                    } else if (reboundAnimator != null && mState.isHeader && (mState.isDragging || mState == RefreshState.RefreshReleased)) {
                        //autoRefresh 正在执行,但未结束
                        //mViceState = RefreshState.None;
                        final ValueAnimator animator = reboundAnimator;
                        reboundAnimator = null;
                        animator.cancel();
//                        resetStatus();
                        mKernel.setState(RefreshState.None);
                    } else if (mState == RefreshState.Refreshing && mRefreshHeader != null && mRefreshContent != null) {
                        count++;
                       //mHandler在为子线程
                        **mHandler.postDelayed(this, more);**
                        //提前设置 状态为 RefreshFinish 防止 postDelayed 导致 finishRefresh 过后,外部判断 state 还是 Refreshing
                        notifyStateChanged(RefreshState.RefreshFinish);
                        if (noMoreData != null) {
                            setNoMoreData(noMoreData == Boolean.TRUE);
                        }
                    }
                } else {
                    int startDelay = mRefreshHeader.onFinish(SmartRefreshLayout.this, success);
                    if (mOnMultiPurposeListener != null && mRefreshHeader instanceof RefreshHeader) {
                        mOnMultiPurposeListener.onHeaderFinish((RefreshHeader) mRefreshHeader, success);
                    }
                    //startDelay < Integer.MAX_VALUE 表示 延时 startDelay 毫秒之后,回弹关闭刷新
                    if (startDelay < Integer.MAX_VALUE) {
                        //如果正在拖动的话,偏移初始点击事件 【两种情况都是结束刷新时,手指还按住屏幕不放手哦】
                        if (mIsBeingDragged || mNestedInProgress) {
                            long time = System.currentTimeMillis();
                            if (mIsBeingDragged) {
                                mTouchY = mLastTouchY;
                                mTouchSpinner = 0;
                                mIsBeingDragged = false;
                                SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_DOWN, mLastTouchX, mLastTouchY + mSpinner - mTouchSlop * 2, 0));
                                SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_MOVE, mLastTouchX, mLastTouchY + mSpinner, 0));
                            }
                            if (mNestedInProgress) {
                                mTotalUnconsumed = 0;
                                SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_UP, mLastTouchX, mLastTouchY, 0));
                                mNestedInProgress = false;
                                mTouchSpinner = 0;
                            }
                        }
                        if (mSpinner > 0) {
                            AnimatorUpdateListener updateListener = null;
                            ValueAnimator valueAnimator = animSpinner(0, startDelay, mReboundInterpolator, mReboundDuration);
                            if (mEnableScrollContentWhenRefreshed) {
                                updateListener = mRefreshContent.scrollContentWhenFinished(mSpinner);
                            }
                            if (valueAnimator != null && updateListener != null) {
                                valueAnimator.addUpdateListener(updateListener);
                            }
                        } else if (mSpinner < 0) {
                            animSpinner(0, startDelay, mReboundInterpolator, mReboundDuration);
                        } else {
                            mKernel.moveSpinner(0, false);
//                            resetStatus();
                            mKernel.setState(RefreshState.None);
                        }
                    }
                }
            }
        };
        if (delay > 0) {
            mHandler.postDelayed(runnable, delay);
        } else {
            runnable.run();
        }
        return this;
    }

问题重现
new AsyncLayoutInflater(activity()).inflate(
mComponentLife.getLayout(),
contentLayout,
new AsyncLayoutInflater.OnInflateFinishedListener() {
@OverRide
public void onInflateFinished(
@nonnull View view,
int i,
@nullable ViewGroup viewGroup) {
contentLayout.addView(view);
}
});
预期行为
指定Handler为主线程Looper

public SmartRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);

    ViewConfiguration configuration = ViewConfiguration.get(context);

    mHandler = new Handler(**Looper.getMainLooper()**);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions