【发布时间】:2020-06-11 09:34:17
【问题描述】:
我正在使用 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 数据库查询代码,它以JSON 格式返回。
get_cmets.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);
?>
这些是我觉得需要的代码...我没有添加适配器和工厂,但如果需要,我可以添加其他代码...有人请帮忙,因为我已经做了几天了.. .
"
【问题讨论】:
-
您的数据源已经是异步的,因此您可以使用 call.execute() 使改造调用同步。
-
php 中 $totalpages 变量中有多少页
-
@NikolaSrdoč 我不明白??
-
@VivekSingh totalpages 在 php 中返回 1。通常,总页数是总/限制...在我的情况下,它是 2/10 = 0.2....但是由于当前页面大于 0.2...$totalpages 从 PHP 代码中设置为 1... .
-
@Ibramazin CommentDataSource 中的加载方法在后台线程中运行。这意味着您应该在该线程上进行同步工作
标签: java android android-studio android-paging