Android 分页库不会停止在 recyclerview 中加载项目

Android Pagination Library wont stop loading items in recyclerview

我正在使用 Android 分页库...它的工作方式很奇怪 - 项目会无限加载,即使没有更多数据要加载也不会停止。

例如,我有 2 个项目要加载,而不是只加载 2 个项目然后停止加载,它一直重复加载 2 个项目...这是我面临的 gif。

首先是我的仓库

CommentDataSource.java

注意 - 当我将“PAGE_SIZE”的值更改为“1”时,它似乎有效但是分页的全部目的都被打败了,因为所有的项目都是一次加载的。 (我测试了更多项目)

public class CommentDataSource extends PageKeyedDataSource<Long, Comment> {
public static int PAGE_SIZE = 10;
public static long FIRST_PAGE = 1;

 public CommentDataSource(){
  }

 @Override
 public void loadInitial(@NonNull final LoadInitialParams<Long> params, @NonNull final 
 LoadInitialCallback<Long, Comment> callback) {
 RestApi restApi = RetrofitApi.create();

 Call<CommentResponse> call = restApi.getComments(FIRST_PAGE);

 call.enqueue(new Callback<CommentResponse>() {
        @Override
        public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
            CommentResponse commentResponse = response.body();
            if(commentResponse!=null){
                progress_bar.postValue(false);
                List<Comment> responseItems = commentResponse.getCommentResponses();
                callback.onResult(responseItems, null, FIRST_PAGE + 1);
            }
        }

   @Override
public void loadBefore(@NonNull final LoadParams<Long> params, @NonNull final LoadCallback<Long, Comment> callback) {
    RestApi restApi = RetrofitApi.create();

    Call<CommentResponse> call = restApi.getComments(params.key);

    call.enqueue(new Callback<CommentResponse>() {
        @Override
        public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
            CommentResponse commentResponse = response.body();
            if(commentResponse!=null){
                progress_bar.postValue(false);
                List<Comment> responseItems = commentResponse.getCommentResponses();
                long key;
                if(params.key > 1){
                    key = params.key - 1;
                }
                else {
                    key = 0;
                }
                callback.onResult(responseItems, key);
            }
        }
    });

}

@Override
public void loadAfter(@NonNull final LoadParams<Long> params, @NonNull final LoadCallback<Long, Comment> callback) {

    RestApi restApi = RetrofitApi.create();

    Call<CommentResponse> call = restApi.getComments(params.key);

    call.enqueue(new Callback<CommentResponse>() {
        @Override
        public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
            CommentResponse commentResponse = response.body();
            if(commentResponse!=null){
                progress_bar.postValue(false);
                List<Comment> responseItems = commentResponse.getCommentResponses();
                callback.onResult(responseItems, params.key + 1);
            }
        }

    });
 }
}

这是我的 ViewModel

CommentViewModel.java

public class CommentViewModel extends ViewModel {
public CommentViewModel(){
    CommentDataFactory commentDataFactory = new CommentDataFactory();
    liveDataSource = commentDataFactory.commentLiveDataSource;

    progressBar = Transformations.switchMap(liveDataSource, CommentDataSource::getProgressBar);

    PagedList.Config config = new PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setPageSize(CommentDataSource.PAGE_SIZE)
            .build();

    commentPagedList = new LivePagedListBuilder<>(commentDataFactory, config).build();
}


public LiveData<PagedList<Comment>> getCommentData(){
    return commentPagedList;
}

public void getRefreshedData(){
    getCommentData().getValue().getDataSource().invalidate();
   } 
 }

这是我的 PHP 数据库查询代码,returns JSON 格式。

get_comments.php

<?php
  $limit = 10;

  //find out how many rows are in the table
  $sql = "SELECT count(*) FROM comments"; 
  $result = $conn->prepare($sql); 
  $result->execute();
  $total = $result->fetchColumn();
  $totalpages = ceil($total/$limit);

  if (isset($_GET['page']) && is_numeric($_GET['page'])) {
  $currentpage =  $_GET['page'];
  } else {
  // default page num
  $currentpage = 1;
  }

if ($currentpage > $totalpages) {
 // set current page to last page
 $currentpage = $totalpages;
 }

 // if current page is less than first page... 
if ($currentpage < 1) {
  $currentpage = 1; 
 } 

 //the offset of the list, based on current page 
 $offset = ($currentpage - 1) * $limit;


  $stmt = "SELECT * FROM comments ORDER by id DESC LIMIT $offset, $limit";
  $result = $conn->prepare($stmt); 
  $result->execute();

  $response = array(); 

  while ($row = $result->fetch()) {

  $temp['id']=$row['id'];
  $temp['image_id']=$row['image_id'];
  $temp['comment']=$row['comment'];
  $temp['comment_by']=$row['comment_by'];
  $temp['date_posted']=$row['date_posted'];

  array_push($response, $temp);

  $obj = (object) [ 
 'data' =>  $response
  ];

 }
echo json_encode($obj); 
?>

这些是我觉得需要的代码...我没有添加适配器和工厂,但如果需要,我可以添加其他代码...有人请帮忙,因为我已经处理了好几天了...

我想我明白为什么它会一次又一次地加载,原因如下:

首先,在 CommentDataSourceloadInitial() 中,您在 onReponse()callback.onResult(responseItems, null, FIRST_PAGE + 1); 行传递了 FIRST_PAGE + 1,这意味着在 loadAfter() FIRST_PAGE + 1 在行 restApi.getComments(params.key) 中作为页码 2 传递,但数据库中不存在页码 2,如果页码大于 1,则设置相同的页码在你的 php

if ($currentpage > $totalpages) {
 // set current page to last page
 $currentpage = $totalpages;
 }

所以你一次又一次地调用第 1 页。在 loadAfter() 中,您一次又一次地调用第 1 页。