我正在使用
Paging Library
和Android架构组件。我只是想观察页面列表的实时数据,并在有变化时更新我的RecyclerView。
我正在观察isLoadingLiveData、isEmptyLiveData和errorLiveData对象,它们是在我的ViewModel中创建的中介LiveData对象,在我的片段中观察到。还观察resultLiveData,它返回从远程获取的Gist列表。
在我的ViewModel中,我创建了一个页面列表LiveData,每当它的数据发生更改时,我都想更新isLoadingLiveData、isEmptyLiveData、errorLiveData和
PagedListAdapter
. 因此,我将isLoadingLiveData、isEmptyLiveData、errorLiveData和resultLiveData定义为MediatorLiveData对象。我添加了resultLiveData作为这些对象的源。因此,当resultLiveData发生更改时,将调用这些对象的onChanged方法。resultLiveData依赖于userNameLiveData,因此当userNameLiveData发生更改时,将调用AllGistLiveData并获取数据。例如,当用户滑动列表时,我设置userNameLiveData并再次进行网络调用。
private val userNameLiveData = MutableLiveData<String>()
private var gists: LiveData<PagedList<Gist>>? = null
val allGistsLiveData: LiveData<PagedList<Gist>>
get() {
if (null == gists) {
gists = GistModel(NetManager(getApplication()), getApplication()).getYourGists(userNameLiveData.value!!).create(0,
PagedList.Config.Builder()
.setPageSize(PAGED_LIST_PAGE_SIZE)
.setInitialLoadSizeHint(PAGED_LIST_PAGE_SIZE)
.setEnablePlaceholders(PAGED_LIST_ENABLE_PLACEHOLDERS)
.build())
}
return gists!!
}
val resultLiveData = MediatorLiveData<LiveData<PagedList<Gist>>>().apply {
this.addSource(userNameLiveData) {
gists = null
this.value = allGistsLiveData
}
}
val isLoadingLiveData = MediatorLiveData<Boolean>().apply {
this.addSource(resultLiveData) { this.value = false }
}
val isEmptyLiveData = MediatorLiveData<Boolean>().apply {
this.addSource(resultLiveData) { this.value = false }
}
val errorLiveData = MediatorLiveData<Boolean>().apply {
this.addSource(resultLiveData) {
if (it == null) {
this.value = true
}
}
}
fun setUserName(userName: String) {
userNameLiveData.value = userName
}
还有我的片段:
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.isEmptyLiveData.observe(this@YourGistsFragment, Observer<Boolean> { isVisible ->
emptyView.visibility = if (isVisible!!) View.VISIBLE else View.GONE
})
viewModel.isLoadingLiveData.observe(this@YourGistsFragment, Observer<Boolean> {
it?.let {
swipeRefreshLayout.isRefreshing = it
}
})
viewModel.errorLiveData.observe(this@YourGistsFragment, Observer<Boolean> {
it?.let {
showSnackBar(context.getString(R.string.unknown_error))
}
})
viewModel.setUserName("melomg")
viewModel.resultLiveData.observe(this@YourGistsFragment, Observer { it -> gistAdapter.setList(it?.value) })
}
override fun onRefresh() {
viewModel.setUserName("melomg")
}
我的存储库:
fun getYourGists(userName: String): LivePagedListProvider<Int, Gist> {
return remoteDataSource.getYourGists(userName, GitHubApi.getGitHubService(context))
}
我的remoteDataSource:
fun getYourGists(username: String, dataProvider: GitHubService): LivePagedListProvider<Int, Gist> {
return object : LivePagedListProvider<Int, Gist>() {
override fun createDataSource(): GistTiledRemoteDataSource<Gist> = object : GistTiledRemoteDataSource<Gist>(username, dataProvider) {
override fun convertToItems(items: ArrayList<Gist>?): ArrayList<Gist>? {
return items
}
}
}
}
我试图从
this project
. 但我的问题是
resultLiveData在没有等待网络调用响应的情况下一直在更改,因此响应的结果被忽略,我的ui在数据到达之前被更新。由于resultLiveData在请求之前发生更改,因此还没有数据。简单地说,我如何观察页面列表的实时数据?