Java Android分页库不会停止在recyclerview中加载项目
我正在使用Android分页库。。。它以一种奇怪的方式工作——项目一直在无限地加载,即使没有更多的数据要加载,也没有停止 例如,我有2个项目要加载,而不是只加载2个项目并停止加载,它会不断重复加载2个项目。。。这是我面临的一个gif 首先,这里是我的存储库 CommentDataSource.javaJava Android分页库不会停止在recyclerview中加载项目,java,android,android-studio,android-paging,Java,Android,Android Studio,Android Paging,我正在使用Android分页库。。。它以一种奇怪的方式工作——项目一直在无限地加载,即使没有更多的数据要加载,也没有停止 例如,我有2个项目要加载,而不是只加载2个项目并停止加载,它会不断重复加载2个项目。。。这是我面临的一个gif 首先,这里是我的存储库 CommentDataSource.java public class CommentViewModel extends ViewModel { public CommentViewModel(){ CommentDataFacto
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();
}
}
注意-当我将“PAGE_SIZE”的值更改为“1”时,它似乎起作用,但分页的整个目的失败了,因为所有项目都是一次加载的。(我测试了更多项目)
这是我的PHP
数据库查询代码,它以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);
?>
这些是我觉得需要的代码…我没有添加适配器和工厂,但如果需要,我可以添加其他代码…有人请帮助,因为我已经在这几天了
“我想我明白了为什么它一次又一次地加载,原因如下: 在
CommentDataSource的loadInitial()
中首先传递FIRST_PAGE+1
行callback.onResult(responseItems,null,FIRST_PAGE+1);
的onReponse()
,这意味着在loadAfter()中
FIRST\u PAGE+1
在restApi.getComments(params.key)
行中作为第2页传递,但第2页不存在于数据库中,如果php中的页码大于1,则设置相同的页码
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
}
因此,您正在一次又一次地调用第1页。在
loadAfter()
中,您正在一次又一次地调用第1页。您的数据源已经是异步的,因此您可以使用call.execute()同步改装调用.中的$totalpages变量中有多少页php@NikolaSrdoč我不明白???@VivekSingh totalpages在php中返回1。通常,总页面数是total/limit…在我的例子中是2/10=0.2…但是由于当前页面大于0.2…$totalpages在php代码中设置为1…@Ibramazin在CommentDa的加载方法中taSource正在后台线程中运行。这意味着您应该在该线程上执行同步工作它现在可以工作了…如果($currentpage>$totalpages){//将当前页面设置为最后一页$currentpage=$totalpages;}谢谢。。。。
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
}