[CymChad/BaseRecyclerViewAdapterHelper]上拉加载更多时,无限触发onLoadMoreRequest,#983和#1094已经尝试过,未解决

2024-08-08 467 views
4

如题 版本:2.9.25 我使用了一个包装类来封装了一下,布局使用Framlayout包裹了RecyclerView和Swiperefreshlayout, 1.布局的代码如下 ` <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipreLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</android.support.v4.widget.SwipeRefreshLayout>

2.包装类,对RecyclerView和SwipeRefreshLayout进行了封装 MyRecyclerView extends FrameLayout implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener `

3.包装类的初始化如下 private void initView(AttributeSet attrs, Context context) { View view = LayoutInflater.from(context).inflate(R.layout.layout_my_refresh, this, true); swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipreLayout); recycler = (RecyclerView) view.findViewById(R.id.recycler); swipeRefreshLayout.setOnRefreshListener(this); swipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189)); recycler.setLayoutManager(new LinearLayoutManager(context)); }

public void setAdapter(A adapter) {
    mAdapter = adapter;
    mAdapter.setOnLoadMoreListener(this, recycler);
    recycler.setAdapter(adapter);
    mAdapter.setLoadMoreView(new CustomLoadMoreView());
}

` 4.下拉刷新和上拉加载的逻辑如下 @Override public void onRefresh() { mOnRefreshListener.onPullDownToRefresh(); }

@Override
public void onLoadMoreRequested() {
    swipeRefreshLayout.setEnabled(false);
    if (mAdapter.getData().size() < PAGE_SIZE) {
        mAdapter.loadMoreEnd(false);
    } else {
        if (mCurrentCounter >= TOTAL_COUNTER) {

            mAdapter.loadMoreEnd(true);//true is gone,false is visible
        } else {

            mOnRefreshListener.onLoadMoreRefresh();
            mCurrentCounter = mAdapter.getData().size();
            mAdapter.loadMoreComplete();
        }
        swipeRefreshLayout.setEnabled(true);
    }
}

`

5.adapter的布局如下 ` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:layout_gravity="center" android:orientation="horizontal">

<TextView
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/tv_stock_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="TEST1" />

<TextView
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/tv_stsock_code"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="TEST2" />

<TextView
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/tv_stock_price"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="TEST3" />

`

回答

9

我这边也是无限触发上拉加载的回调,因为mAdapter.getData().size()一直都没有改变,不知道为什么

8

你添加数据的代码在哪里?@huang3eng 要用adapter添加数据mAdapter.getData().size()就会变呀

8

如果不添加数据一直调用mAdapter.loadMoreComplete();肯定会无限加载

3

@CymChad 我加载更多是通过接口 mOnRefreshListener.onLoadMoreRefresh(); 然后在外面调用去请求数据,但是在这下面紧接着mOnRefreshListener.onLoadMoreRefresh(); mCurrentCounter = mAdapter.getData().size(); mAdapter.loadMoreComplete(); 不知道是不是这里的问题,mOnRefreshListener.onLoadMoreRefresh()的回调是去请求数据,这里的getData,size是否需要放到数据请求回来之后的回调?

4

mOnRefreshListener.onLoadMoreRefresh(); 这个里面是异步的操作你应该把后面的东西全部放在数据加载成功后在去调用呀。

0

@CymChad 数据返回时。 if (isRefresh){ mAdapter.setNewData(result.getDataList()); //disableLoadMore...请在setNewData后调用,不是配置项。 mAdapter.disableLoadMoreIfNotFullPage(); }else { Log.e("tag","onTaskFinished" + result.getDataList().size()); mAdapter.addData(result.getDataList()); if (isErr){ // 加载更多数据,出错时 mAdapter.loadMoreFail(); } mRefreshLayout.setEnabled(true); } mRefreshLayout.stopRefresh(); mRecyclerView.setVisibility(View.VISIBLE); onRefreshCompleted(result);

加载更多回调: @Override public void onLoadMoreRequested() { isRefresh = false; // 当列表item数量小于一页时,或者当item数量大于全部数量时,加载更多结束。loadMoreEnd if (mAdapter.getData().size() < pageSize || mAdapter.getData().size() >= total_count ){ mAdapter.loadMoreEnd(true); }else if (mAdapter.getItemCount()< total_count){ // 当列表数量小于全部数量切大于一页数量,这次加载更多结束, Log.e("tag","onLoadMoreRequested"+"," +mAdapter.getData().size()+"," + total_count + "," + mAdapter.getItemCount()); pageAt ++; loadingData(); mAdapter.loadMoreComplete(); } }

9

@huang3eng loadingData();获取数据是异步操作,你应该加载成功数据添加完毕后在调用 mAdapter.loadMoreComplete();

8

@CymChad 问题解决了,谢谢老大。

6

@CymChad 多谢大佬,解决了,确实是因为加载在子线程,需要把complete的设置放在回调里面

7

@huang3eng @jianfeng318 感谢使用,如果觉得好用请分享给你身边的开发者朋友。