所以我一直在阅读 Android 的新分页库。具体来说,我正在尝试使用带有适配器、视图模型和数据源(由改造网络调用支持)的 recyclerview 加载一个永无止境的图像列表(使用 Glide)。
这是基本代码:
// inside my activity
PhotosListAdapter adapter = new PhotosListAdapter(getApplicationContext());
PhotosViewModel viewModel = ViewModelProviders.of(this).get(PhotosViewModel.class);
viewModel.getPhotosList().observe(this, adapter::submitList);
RecyclerView recyclerView = findViewById(R.id.main_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
...
// My adapter code:
@Override
public void onBindViewHolder(@NonNull PhotoViewHolder photoViewHolder, int i) {
Photo photo = getItem(i);
if (photo != null) {
mGlide.load(photo.previewURL).into(photoViewHolder.mPhotoImageView);
}
}
private static final DiffUtil.ItemCallback<Photo> DIFF_CALLBACK = new DiffUtil.ItemCallback<Photo>() {
@Override
public boolean areItemsTheSame(@NonNull Photo photo, @NonNull Photo other) {
return photo.id == other.id; // these are just ints
}
@Override
public boolean areContentsTheSame(@NonNull Photo photo, @NonNull Photo other) {
return photo.previewURL.equals(other.previewURL); // this is just a string
}
};
...
// My View Model code:
private LiveData<PagedList<Photo>> mData;
public PhotosViewModel() {
PhotosDataSource photosDataSource = new PhotosDataSource();
mData = new LivePagedListBuilder<>(new DataSource.Factory<Integer, Photo>() {
@Override
public DataSource<Integer, Photo> create() {
return photosDataSource;
}
}, 25).build();
}
public LiveData<PagedList<Photo>> getPhotosList() {
return mData;
}
// My Data Source:
Call<SearchResult> search = mPixabayService.search(PixabayApi.api_key, "photo", 1, params.requestedLoadSize, true);
search.enqueue(new Callback<SearchResult>() {
@Override
public void onResponse(Call<SearchResult> call, Response<SearchResult> response) {
if (response.isSuccessful()) {
SearchResult body = response.body();
callback.onResult(body.hits, null, 2);
}
// TODO add error cases
}
@Override
public void onFailure(Call<SearchResult> call, Throwable t) {
}
});
@Override
public void loadAfter(@NonNull LoadParams params, @NonNull LoadCallback callback) {
Call<SearchResult> result = mPixabayService.search(PixabayApi.api_key, "photo", (Integer)params.key, params.requestedLoadSize, true);
result.enqueue(new Callback<SearchResult>() {
@Override
public void onResponse(Call<SearchResult> call, Response<SearchResult> response) {
if (response.isSuccessful()) {
List<Photo> hits = response.body().hits;
callback.onResult(hits, (Integer)params.key + 1);
}
}
@Override
public void onFailure(Call<SearchResult> call, Throwable t) {
}
});
}
但是,我只使用静态项目列表适配器构建了相同的示例,这似乎在我的模拟器上滚动得更顺畅。我的代码中是否缺少某些内容?
以下是我对可能使情况变得更糟的因素的怀疑:
- 在这种情况
recyclerview.setHasFixedSize(true)
下有意义吗?(当我向下滚动然后返回原始图像时,我确实注意到图像的大小不同 - 有没有办法防止这种情况?) - 这是因为一些奇怪的东西
DIFF_CALLBACK
吗?我其实不太清楚这门课的目的 viewModel.getPhotosList().observe(this, adapter::submitList);
预计会更慢,因为它应该使列表无效(与静态列表相比);但是,这是否会导致 5 倍的延迟??!- 我注意到我在模拟器上飞得很快,这在静态回收器视图上似乎不是问题;但是,LivePage 是否为他们的投掷检测做了一些不同的事情?当我扔东西时,屏幕似乎发疯了。换句话说,我感知到的“分页库的锯齿状性能”仅仅是因为我在上下摆动(而不是只是缓慢地上下滚动)吗?
- 我只是在某处错过了关键配置设置吗?