Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

调用loadMoreComplete()会造成无限循环加载更多 #1704

Closed
goocher opened this issue Nov 3, 2017 · 11 comments
Closed

调用loadMoreComplete()会造成无限循环加载更多 #1704

goocher opened this issue Nov 3, 2017 · 11 comments

Comments

@goocher
Copy link

goocher commented Nov 3, 2017

如下源码方法:

public void loadMoreComplete() {
        if (getLoadMoreViewCount() == 0) {
            return;
        }
        mLoading = false;
        mNextLoadEnable = true;
        mLoadMoreView.setLoadMoreStatus(LoadMoreView.STATUS_DEFAULT);
        notifyItemChanged(getLoadMoreViewPosition());
    }

当调用notifyItemChanged(getLoadMoreViewPosition());时又会调用onBindViewHolder()方法
此时关键代码来了,此时又重新调用了 autoLoadMore(position);方法,该方法就会出问题

private void autoLoadMore(int position) {
        if (getLoadMoreViewCount() == 0) {
            return;
        }
        if (position < getItemCount() - mPreLoadNumber) {
            return;
        }
        if (mLoadMoreView.getLoadMoreStatus() != LoadMoreView.STATUS_DEFAULT) {
            return;
        }
        mLoadMoreView.setLoadMoreStatus(LoadMoreView.STATUS_LOADING);
        if (!mLoading) {
            mLoading = true;
            if (getRecyclerView() != null) {
                getRecyclerView().post(new Runnable() {
                    @Override
                    public void run() {
                        mRequestLoadMoreListener.onLoadMoreRequested();
                    }
                });
            } else {
                mRequestLoadMoreListener.onLoadMoreRequested();
            }
        }
    }

由于此时调用了loadMoreComplete()方法,mLoading处于false状态,由此会进入下边的判断,造成无限回掉加载更多。。。。。。。这就尴尬了!不光这个方法,loadMoreEnd()....记不清楚了那几个结束加载的方法也是如此,
我遇到的问题场景:对于RecyclerView的item我没有设置满一屏幕,而我设置了加载更多监听,在里边我就设置了上拉加载添加数据,结果竟然有那么多数据,忘记说了我也设置了不满一屏幕时不加载更多虽然有顺序问题,因为在你设置监听时就会开启加载更多,说的有点乱,看一下代码吧!希望有帮助!

 private void init(View view) {
        mRvContent = view.findViewById(R.id.rv_main_content);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRvContent.setLayoutManager(layoutManager);
        ArrayList<CardBean.CardListEntity> data = new ArrayList<>();
        mAdapter = new SingleItemAdapter(R.layout.my_item, data);
        mRvContent.setAdapter(mAdapter);
        initData();
        if (mAdapter.isLoadMoreEnable()) {
           mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
              mAdapter.addData(mCardBean.cardList);
                    mAdapter.loadMoreEnd();
                }
            }, mRvContent);
        }
    }
private void initData() {
        String json = CommenUtils.getJson(NewsApp.getmContext(), "cardlist.json");
        mCardBean = CommenUtils.jsonToBean(json, CardBean.class);
        mAdapter.replaceData(mCardBean.cardList);
        mAdapter.setPreLoadNumber(8);
        mAdapter.disableLoadMoreIfNotFullPage(mRvContent);
    }

上边的mAdapter.disableLoadMoreIfNotFullPage(mRvContent);在这设置没有一点卵用。希望有更好的写法的小伙伴指出不足!

@buleSkyvv
Copy link

我也遇到这个问题 ,是在SV中嵌套RV时候出现的,在怎么调试都是同样的问题,mloading一直处于false的状态会自动走autoloadmore(pos)这个方法,无限循环。希望作者能处理下

@1109993488
Copy link
Collaborator

disableLoadMoreIfNotFullPage是跟setNewData一起使用的,只有在没数据第一次设置的时候是有用的

@1109993488
Copy link
Collaborator

没有数据的时候需要调用loadMoreEnd,如果调了这个就不会回调下一页,请确认是否是在没有数据的时候调了loadMoreEnd方法

@1109993488
Copy link
Collaborator

@buleSkyvv SV跟RV有冲突,建议添加header方式解决

@Ryan2Archibald
Copy link

同上,只有在SV中使用RV时,会自动无限加载,没有找到合适的解决方案,期望新的更新解决

@CymChad
Copy link
Owner

CymChad commented Nov 8, 2017

@RyanChouChina @buleSkyv 这个SV嵌套RV是加载机制上的冲突,目前无法解决,建议添加header方式解决或者使用其他上拉的嵌套库。

@RookieJay
Copy link

RookieJay commented Nov 13, 2019

loadMoreEnd

loadMoreEnd是不是必须在setOnLoadMoreListener监听里面调用呢,如果我返回数据为空,在showData的时候调用loadMoreEnd还是会一直重复调用加载更多的方法。

@longertime
Copy link

loadMoreEnd

loadMoreEnd是不是必须在setOnLoadMoreListener监听里面调用呢,如果我返回数据为空,在showData的时候调用loadMoreEnd还是会一直重复调用加载更多的方法。

我也是这样?请问有解决方案吗?确实mloading这个值一直处于fasle状态,导致onbindviewholder一直调用autoloadmore()这个方法

@longertime
Copy link

如下源码方法:

public void loadMoreComplete() {
        if (getLoadMoreViewCount() == 0) {
            return;
        }
        mLoading = false;
        mNextLoadEnable = true;
        mLoadMoreView.setLoadMoreStatus(LoadMoreView.STATUS_DEFAULT);
        notifyItemChanged(getLoadMoreViewPosition());
    }

当调用notifyItemChanged(getLoadMoreViewPosition());时又会调用onBindViewHolder()方法
此时关键代码来了,此时又重新调用了 autoLoadMore(position);方法,该方法就会出问题

private void autoLoadMore(int position) {
        if (getLoadMoreViewCount() == 0) {
            return;
        }
        if (position < getItemCount() - mPreLoadNumber) {
            return;
        }
        if (mLoadMoreView.getLoadMoreStatus() != LoadMoreView.STATUS_DEFAULT) {
            return;
        }
        mLoadMoreView.setLoadMoreStatus(LoadMoreView.STATUS_LOADING);
        if (!mLoading) {
            mLoading = true;
            if (getRecyclerView() != null) {
                getRecyclerView().post(new Runnable() {
                    @Override
                    public void run() {
                        mRequestLoadMoreListener.onLoadMoreRequested();
                    }
                });
            } else {
                mRequestLoadMoreListener.onLoadMoreRequested();
            }
        }
    }

由于此时调用了loadMoreComplete()方法,mLoading处于false状态,由此会进入下边的判断,造成无限回掉加载更多。。。。。。。这就尴尬了!不光这个方法,loadMoreEnd()....记不清楚了那几个结束加载的方法也是如此,
我遇到的问题场景:对于RecyclerView的item我没有设置满一屏幕,而我设置了加载更多监听,在里边我就设置了上拉加载添加数据,结果竟然有那么多数据,忘记说了我也设置了不满一屏幕时不加载更多虽然有顺序问题,因为在你设置监听时就会开启加载更多,说的有点乱,看一下代码吧!希望有帮助!

 private void init(View view) {
        mRvContent = view.findViewById(R.id.rv_main_content);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRvContent.setLayoutManager(layoutManager);
        ArrayList<CardBean.CardListEntity> data = new ArrayList<>();
        mAdapter = new SingleItemAdapter(R.layout.my_item, data);
        mRvContent.setAdapter(mAdapter);
        initData();
        if (mAdapter.isLoadMoreEnable()) {
           mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
              mAdapter.addData(mCardBean.cardList);
                    mAdapter.loadMoreEnd();
                }
            }, mRvContent);
        }
    }
private void initData() {
        String json = CommenUtils.getJson(NewsApp.getmContext(), "cardlist.json");
        mCardBean = CommenUtils.jsonToBean(json, CardBean.class);
        mAdapter.replaceData(mCardBean.cardList);
        mAdapter.setPreLoadNumber(8);
        mAdapter.disableLoadMoreIfNotFullPage(mRvContent);
    }

上边的mAdapter.disableLoadMoreIfNotFullPage(mRvContent);在这设置没有一点卵用。希望有更好的写法的小伙伴指出不足!

老哥解决这个问题了吗?

@RookieJay
Copy link

@longertime 我现在是这样解决的 在加载更多监听这里判断了下页码
image
image

@longertime
Copy link

@longertime 我现在是这样解决的 在加载更多监听这里判断了下页码
image
image

嗯,可以这么判断,但是我这里需求有点特殊,总页数是变化的,当前页数也是变化的。。。。不过我感觉你的方案是可以的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants