android-cjj / Android-MaterialRefreshLayout

This is a drop-down control, it is more beautiful and powerful than SwipeRefreshLayout
2.11k stars 483 forks source link

加载更多,执行两次或更多,什么原因 #56

Open hbgrape opened 8 years ago

hbgrape commented 8 years ago

加载更多,执行两次或更多,什么原因

WqyJh commented 7 years ago

我也碰到这个问题,原因是刷新耗时比较短,在上拉动作还没结束的时候就完成了,此时的上拉动作触发了下一次刷新. 查看 MaterialRefreshLayout.java 的代码发现, 加载更多的回调方法 onRefreshLoadMore() 是在 onInterceptTouchEvent() 里的 MotionEvent.ACTION_MOVE 分支里调用的, 因此只要还在滑动,就有可能触发加载更多.

  @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (isRefreshing) return true;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchY = ev.getY();
                mCurrentY = mTouchY;
                break;
            case MotionEvent.ACTION_MOVE:
                float currentY = ev.getY();
                float dy = currentY - mTouchY;
                if (dy > 0 && !canChildScrollUp()) {
                    if (mMaterialHeaderView != null) {
                        mMaterialHeaderView.setVisibility(View.VISIBLE);
                        mMaterialHeaderView.onBegin(this);
                    } else if (mSunLayout != null) {
                        mSunLayout.setVisibility(View.VISIBLE);
                        mSunLayout.onBegin(this);
                    }
                    return true;
                } else if (dy < 0 && !canChildScrollDown() && isLoadMore) {
                    if (mMaterialFooterView != null && !isLoadMoreing) {
                        soveLoadMoreLogic();
                    }
                    return super.onInterceptTouchEvent(ev);
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

解决: 将事件派发的逻辑放到 MotionEvent.ACTION_UP 分支里, 亲测可行. 本人对代码的其它逻辑不是很了解, 所以也不能确定这样不会造成其它问题, 希望作者看到可以解决一下这个问题, 谢谢啦.

 @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (isRefreshing) return true;
        float currentY = ev.getY();
        float dy = currentY - mTouchY;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchY = ev.getY();
                mCurrentY = mTouchY;
                break;
            case MotionEvent.ACTION_UP:
                if (dy < 0 && !canChildScrollDown() && isLoadMore) {
                    if (mMaterialFooterView != null && !isLoadMoreing) {
                        soveLoadMoreLogic();
                    }
                    return super.onInterceptTouchEvent(ev);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (dy > 0 && !canChildScrollUp()) {
                    if (mMaterialHeaderView != null) {
                        mMaterialHeaderView.setVisibility(View.VISIBLE);
                        mMaterialHeaderView.onBegin(this);
                    } else if (mSunLayout != null) {
                        mSunLayout.setVisibility(View.VISIBLE);
                        mSunLayout.onBegin(this);
                    }
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
wmailn commented 7 years ago

我也遇到了,列表一碰就触发加载更多 :https://github.com/android-cjj/Android-MaterialRefreshLayout/issues/67