Android分页库不会停止在recyclerview中加载项目 [英] Android Pagination Library wont stop loading items in recyclerview
问题描述
我正在使用Android分页库...它以一种怪异的方式工作-项目无限地加载,即使没有更多数据要加载也没有停止.
I am using Android Pagination Library... It's working in a weird way - Items keep loading infinitely, there is no stop even when there are no more data to load.
例如,我要加载2个项目,而不是仅加载2个项目并停止加载,而是不断重复加载这2个项目...这是我所面对的gif文件.
For example, I have 2 Items to load, instead of loading just the 2 items and stop loading, It keeps loading the 2 items repeatedly... Here is a gif of what I am facing.
首先这是我的存储库
CommentDataSource.java
注意-当我将" PAGE_SIZE "的值更改为" 1 "时,似乎可行,但分页的整个目的却失败了,因为所有项目都被取消了一次加载.(我测试了更多项目)
Note - when I change the value of "PAGE_SIZE" to "1", It seems to work but the whole purpose of the pagination defeated as ALL the items are loaded at once. (I tested with more items)
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
代码,它以 JSON
格式返回.
Here is my PHP
code for database query which returns in JSON
format.
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);
?>
这些是我认为需要的代码...我没有添加适配器和工厂,但如果需要,我可以添加其他代码...在过去的几天里,有人请帮忙.
These are the codes I feel are needed...I didn't add the adapter and the factory but if need be, I can add other codes...Someone pls help as I have been on this for days...
"
推荐答案
我想我知道为什么它会一次又一次地加载,这是为什么:
I think i got why it is loading again and again, here is why:
在您的 CommentDataSource
的 loadInitial()
中显示的内容,您正在 callback.onResult(responseItems)行中传递
的 FIRST_PAGE + 1
,null,FIRST_PAGE + 1); onReponse()
,这意味着在 loadAfter()
中, FIRST_PAGE + 1
在行 restApi.getComments(params.key)
中作为页面编号2传递,但是数据库中不存在页面编号2,并且如果页面编号大于1在您的php中
Firts in the loadInitial()
of your CommentDataSource
you are passing FIRST_PAGE + 1
in the line callback.onResult(responseItems, null, FIRST_PAGE + 1);
of the onReponse()
, that means in the loadAfter()
that FIRST_PAGE + 1
get passed as page number 2 in the line restApi.getComments(params.key)
but page number 2 dosn't exist in your database and you are setting the same page number if the page number is greater than 1 in your php
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
}
因此,您一次又一次呼叫第1页.在 loadAfter()
中,您一次又一次地调用页面1.
So you are calling page number 1 again and again. In the loadAfter()
you are calling the page 1 again and then again and again.
这篇关于Android分页库不会停止在recyclerview中加载项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!