Android 分页库中的 loadAfter() 从 API 无限加载第一页

loadAfter() in Android paging library infinitely loading the first page from API

我正在我的 Android 应用程序中实现分页库 2.1.2,我的数据源 class 看起来像这样

public class TransactionsDataSource extends PageKeyedDataSource<Integer, Items> {


private static final int FIRST_PAGE = 1;
private Context context;
private Repository repository;
private String bearerToken;
private int merchantId;


public TransactionsDataSource(Context context, int merchantId) {
    this.context = context;
    this.merchantId = merchantId;
    repository = new Repository();
    repository.initLoginSharedPref(context);
    bearerToken = repository.getAccessToken();
}


@Override
public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<Integer> params, @NonNull PageKeyedDataSource.LoadInitialCallback<Integer, Items> callback) {


    ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
            merchantId, FIRST_PAGE)
            .enqueue(new Callback<MainResponse>() {
                @Override
                public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
                    if (response.body() != null){

                        int code = response.body().getCode();
                        if (code == SERVER_OK_CODE){
                            callback.onResult(response.body().getData().getItems(), null, FIRST_PAGE + 1);
                        } else if (code == SERVER_UNAUTHENTICATED_CODE){

                            repository.clearCache();
                            HelperMethods.signOut(context);
                        } else {
                            //no more data to load
                        }


                    }
                }

                @Override
                public void onFailure(Call<MainResponse> call, Throwable t) {

                }
            });
}

@Override
public void loadBefore(@NonNull PageKeyedDataSource.LoadParams<Integer> params, @NonNull PageKeyedDataSource.LoadCallback<Integer, Items> callback) {

}

@Override
public void loadAfter(@NonNull PageKeyedDataSource.LoadParams<Integer> params, @NonNull PageKeyedDataSource.LoadCallback<Integer, Items> callback) {


    ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
            merchantId, params.key)
            .enqueue(new Callback<MainResponse>() {
                @Override
                public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
                    if (response.body() != null){

                        int code = response.body().getCode();
                        if (code == SERVER_OK_CODE){

                            Integer key = params.key + 1;
                            callback.onResult(response.body().getData().getItems(), key);

                        } else if (code == SERVER_UNAUTHENTICATED_CODE){

                            repository.clearCache();
                            HelperMethods.signOut(context);
                        } else {
                            //no more data to load
                        }

                    }
                }

                @Override
                public void onFailure(Call<MainResponse> call, Throwable t) {

                }
            });
}

}

据我所知,loadInitial() 方法负责加载第一页数据,然后将触发 loadAfter() 以加载其余页面,同时键递增一个,直到没有要加载更多数据。但是当我调试应用程序时,即使我确保将 params.key() 递增 1,loadAfter() 也会不断重复加载第一页。我什至尝试如下硬编码页码,但仍在加载仅第一页

ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
        merchantId, 2)

我需要知道发生了什么以及如何修复这个错误?

虽然我的实现是 100% 正确的,而且在我尝试了很多东西之后我终于找到了解决这个奇怪行为的方法。我在我的界面中做了一个小改动,而不是将页码传递给 getMerchantTransactions() 我决定将整个 url 作为参数发送,它工作正常 这是更新后界面中的方法

@POST
@FormUrlEncoded
Call<MainResponse> getMerchantTransactions(@Header("Authorization") String bearerToken,
                                           @Field("id") int merchantId,
                                           @Url String url);

然后像这样在loadInitial()和loadAfter()中调用这个方法

ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
            merchantId, "myUrl?pagenumber=" + params.key)