在回收视图中加载图像



在回收器视图中从json收集图像。当我们到达最后时如何加载下一页?我只是在学习使用retrofit2,rxjava2。我请你不要严格判断,并暗示我做错了什么。我将侦听器挂在适配器内的onBind中:

@Override public void onBindViewHolder(@NonNull ImageViewHolder holder, final int position) {
Hit hit = hits.get(position);
Picasso.get().load(hit.getLargeImageURL())
.into(holder.imageViewPreview);
if (loadMoreListener != null && !isLoading && !noMore && position == getItemCount() - 1) {
isLoading = true;
loadMoreListener.loadMore();
}

我正在尝试通过将页面编号传递给参数来实现加载下一页的激活:

adapter.setLoadMoreListener(new ImageAdapter.LoadMoreListener() {
@Override
public void loadMore() {
Toast.makeText(MainActivity.this, "Конец", Toast.LENGTH_SHORT).show();
page++;
viewModel.loadImages(searchQuery, page);
adapter.endLoading();
adapter.setNoMore(true);
adapter.notifyDataSetChanged();
}
});

发生启动错误 - 无法在回收器视图计算布局或滚动 androidx.recyclerview.widget.RecyclerView 时调用此方法

加载图像 (( 方法代码:

public void loadImages(String query, int page){
NetworkService networkService  =  NetworkService.getInstance();
compositeDisposable = new CompositeDisposable();
Disposable disposable = networkService.getJSONApi()
.getResponse(query, page)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Response>() {
@Override
public void accept(Response response) throws Exception {
deleteHits();
insertHits(response.getHits());
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Toast.makeText(getApplication(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
});
compositeDisposable.add(disposable);

}

尝试使用以下代码通知适配器:

recyclerview.post(new Runnable() {
@Override
public void run() {
myadapter.notifyDataSetChanged();
}
});

另请检查:https://www.journaldev.com/24041/android-recyclerview-load-more-endless-scrolling

相关内容

  • 没有找到相关文章

最新更新