我正在使用 PagingDataAdapter 来构建我的 recyclerview,但不知怎的,在 mainactivity android studio 中显示了一个错误,即 SubmitData 不是相应适配器的函数。
这是我在 MainActivity 中的代码:
viewModel = new ViewModelProvider(this ,
ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()))
.get(PhotoDataSourceFactory.class);
Lifecycle lifecycle = getLifecycle();
viewModel.setLifecycle(lifecycle);
recyclerView = findViewById(R.id.recyclerView);
PhotosAdapter adapter = new PhotosAdapter(Photos.CALLBACK);
recyclerView.setAdapter(adapter);
viewModel.getSearchResults().observe(this, listPagingData -> adapter.submitData(lifecycle, listPagingData));
我的视图模型:
public class PhotoDataSourceFactory extends ViewModel {
Lifecycle lifecycle;
private static final String TAG = "PhotoDataSourceFactory";
public PhotoDataSourceFactory() {
}
public void setLifecycle(Lifecycle lifecycle){
this.lifecycle = lifecycle;
}
public LiveData<PagingData<List<Photos>>> getSearchResults(){
PagingConfig config = new PagingConfig(20, 100, false);
Pager<Long , List<Photos>> pager = new Pager<>(config, new Function0<PagingSource<Long, List<Photos>>>() {
@Override
public PagingSource<Long, List<Photos>> invoke() {
return new PhotoSource();
}
});
return PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager),lifecycle);
}
}
我的适配器:
public class PhotosAdapter extends PagingDataAdapter<Photos , PhotosAdapter.PhotoViewHolder> {
private static final String TAG = "PhotosAdapter";
public PhotosAdapter(@NotNull DiffUtil.ItemCallback<Photos> diffCallback) {
super(diffCallback);
}
@NonNull
@Override
public PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.list_photos,parent,false);
return new PhotoViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PhotoViewHolder holder, int position) {
Glide.with(holder.itemView.getContext()).load(getItem(position).getUrl()+".png").into(holder.photo);
holder.textView.setText(getItem(position).getTitle());
}
public class PhotoViewHolder extends RecyclerView.ViewHolder{
ImageView photo;
TextView textView;
public PhotoViewHolder(@NonNull View itemView) {
super(itemView);
photo = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
和我的分页来源:
public class PhotoSource extends ListenableFuturePagingSource<Long , List<Photos>> {
GetDataService dataService;
private static final String TAG = "PhotoSource";
@NotNull
@Override
public ListenableFuture<LoadResult<Long, List<Photos>>> loadFuture(@NotNull LoadParams<Long> loadParams) {
Executor executor = Executors.newSingleThreadExecutor();
dataService = RetrofitInstance.getInstance().create(GetDataService.class);
Long currentPage = loadParams.getKey()!=null ? loadParams.getKey() : (long)1;
ListenableFuture<List<Photos>> photos = dataService.getAllPhotos(currentPage);
ListenableFuture<LoadResult<Long , List<Photos>>> page = Futures.transform(photos, new Function<List<Photos>, LoadResult<Long, List<Photos>>>() {
@NullableDecl
@Override
public LoadResult.Page<Long, List<Photos>> apply(@NullableDecl List<Photos> input) {
return new LoadResult.Page<>(
Collections.singletonList(input),
currentPage == 1 ? currentPage : currentPage-1,
input.isEmpty() ? null : currentPage+1
);
}}, executor);
Log.d(TAG, "loadFuture: Heererereree");
ListenableFuture<LoadResult<Long , List<Photos>>> partialLoad = Futures.catching(page, HttpException.class, LoadResult.Error::new, executor);
return Futures.catching(partialLoad, IOException.class, LoadResult.Error::new, executor);
}
@Nullable
@Override
public Long getRefreshKey(@NotNull PagingState<Long, List<Photos>> pagingState) {
return null;
}
}
我不明白我哪里出了问题,尝试在 stackoverflow 上搜索很多,但都是徒劳, 我无法从 google 关于 paging 3 in java 的文档中收集到任何实质性内容来解决此问题。试图在 java 中找到有关分页 3 的视频,但不幸的是没有人介绍过 Java 中的分页。
如果有人在java中完成了这个(分页3),如果可能的话也请添加一个到你的github的链接 预先感谢:)
我也遇到过类似的问题。就我而言,问题不在于“adapter.submitData()”,而在于实际响应本身。由于我使用挂起函数来获取响应数据,API 的响应出现了问题。因此,请检查 api 的响应以进行调试。
刚刚发现出了问题,如果您想了解 java 中分页 3 的基本概述,请检查下面的代码。
https://github.com/manas-droid/Android-Paging-3/tree/main/app/src/main/java/com/example/photos