我正在为我的应用程序使用最新的 Paging3 库,它有一个显示照片列表的画廊屏幕,以及一个显示更多选项和照片信息的详细信息屏幕。我已经设置画廊来获取我的片段中的照片列表onCreate
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// display all photos, sorted by latest
viewModel.getAllPhotos()
}
如果成功,照片会通过 传递给适配器submitList
,如果用户拉下画廊屏幕,它应该会触发刷新,所以我refreshListener
相应地设置了一个。我这样做onViewCreated
(注意我使用 ViewBinding):
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentGalleryBinding.bind(view)
viewLifecycleOwner.lifecycle.addObserver(viewModel)
setupGallery()
setupRetryButton()
}
private fun setupGallery() {
// Add a click listener for each list item
adapter = GalleryAdapter{ photo ->
photo.id.let {
findNavController().navigate(GalleryFragmentDirections.detailsAction(it))
}
}
viewModel.uiState?.observe(viewLifecycleOwner, {
binding?.swipeLayout?.isRefreshing = false
adapter.submitData(lifecycle, it)
})
binding?.apply {
// Apply the following settings to our recyclerview
list.adapter = adapter.withLoadStateHeaderAndFooter(
header = RetryAdapter {
adapter.retry()
},
footer = RetryAdapter {
adapter.retry()
}
)
// Add a listener for the current state of paging
adapter.addLoadStateListener { loadState ->
Log.d("GalleryFragment", "LoadState: " + loadState.source.refresh.toString())
// Only show the list if refresh succeeds.
list.isVisible = loadState.source.refresh is LoadState.NotLoading
// do not show SwipeRefreshLayout's progress indicator if LoadState is NotLoading
swipeLayout.isRefreshing = loadState.source.refresh !is LoadState.NotLoading
// Show loading spinner during initial load or refresh.
progressBar.isVisible = loadState.source.refresh is LoadState.Loading && !swipeLayout.isRefreshing
// Show the retry state if initial load or refresh fails.
retryButton.isVisible = loadState.source.refresh is LoadState.Error
val errorState = loadState.source.append as? LoadState.Error
?: loadState.source.prepend as? LoadState.Error
?: loadState.append as? LoadState.Error
?: loadState.prepend as? LoadState.Error
errorState?.let {
swipeLayout.isRefreshing = false
Snackbar.make(requireView(),
"\uD83D\uDE28 Wooops ${it.error}",
Snackbar.LENGTH_LONG).show()
}
}
swipeLayout.apply {
setOnRefreshListener {
isRefreshing = true
adapter.refresh()
}
}
}
首次加载时,下拉布局会成功触发刷新。但是,在我导航到详细信息屏幕后出现问题。在详细信息屏幕中,按返回按钮可将用户返回到图库。如果用户拉动布局,进度指示器会出现但adapter.refresh()
不会发生。我不知道如何调试它。
作为参考,这是我ViewModel
负责获取照片的人员的样子:
class GalleryViewModel(private val getAllPhotosUseCase: GetAllPhotosUseCase): BaseViewModel() {
private val _uiState = MutableLiveData<PagingData<UnsplashPhoto>>()
val uiState: LiveData<PagingData<UnsplashPhoto>>? get() = _uiState
fun getAllPhotos() {
compositeDisposable += getAllPhotosUseCase.getAllPhotos()
.cachedIn(viewModelScope)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = { _uiState.value = it },
onError = {
it.printStackTrace()
}
)
}
}
将调用GetAllPhotosUseCase
转发到包含以下内容的实现:getAllPhotos
Repository
class UnsplashRepoImpl(private val unsplashApi: UnsplashApi): UnsplashRepo {
override fun getAllPhotos(): Observable<PagingData<UnsplashPhoto>> = Pager(
config = PagingConfig(Const.PAGE_SIZE),
remoteMediator = null,
// Always create a new UnsplashPagingSource object. Failure to do so would result in a
// IllegalStateException when adapter.refresh() is called--
// Exception message states that the same PagingSource was used as the prev request,
// and a new PagingSource is required
pagingSourceFactory = { UnsplashPagingSource(unsplashApi) }
).observable
....
}
我RxPagingSource
的设置是这样的:
class UnsplashPagingSource (private val unsplashApi: UnsplashApi)
: RxPagingSource<Int, UnsplashPhoto>(){
override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, UnsplashPhoto>> {
val id = params.key ?: Const.PAGE_NUM
return unsplashApi.getAllPhotos(id, Const.PAGE_SIZE, "latest")
.subscribeOn(Schedulers.io())
.map { response ->
response.map { it.toUnsplashPhoto() }
}
.map<LoadResult<Int, UnsplashPhoto>> { item ->
LoadResult.Page(
data = item,
prevKey = if (id == Const.PAGE_NUM) null else id - 1,
nextKey = if (item.isEmpty()) null else id + 1
)
}
.onErrorReturn { e -> LoadResult.Error(e) }
}
}
谁能指出我正确的方向?
编辑:正如 Jay Dangar 所说,移动viewModel.getAllPhotos()
到onResume
会adapter.refresh()
成功触发。但是,我不想每次从详细信息屏幕导航到图库时都获取所有照片。为了避免这种情况,我不调用adapter.refresh()
布局时调用,而是调用viewModel.getAllPhotos()
。
我仍然不明白为什么接受的答案有效,但我认为adapter.refresh()
只有在PagingSource
创建新答案或其他东西时才有效。