分页库的进度条和错误消息
Progressbar and error message with Paging Library
Paging Library 太棒了。但我发现缺少此功能:
- 加载数据时调度视图。假设我正在扩展
PageKeyedDataSource
:调用 loadInitial
时顶部的视图,调用 loadAfter
时列表底部的视图。当 callback
被调用时,视图应该消失。
- 出现错误时调度视图。
- 滑动刷新
因为现在这是不可能的,有谁知道使用 PagingLibrary 的方法吗?至少一种在同一个列表中使用不同视图的方法。
有一个很好的示例@yigit 与 Paging 库相关,它还展示了如何在 Recyclerview 中处理进度条、错误和重试。所以基本上他正在创建一个 Listing.kt
数据 class,它由 NetworkState
和 PagedList
组成,如下所示
Listing.kt
data class Listing<T>(
// the LiveData of paged lists for the UI to observe
val pagedList: LiveData<PagedList<T>>,
// represents the network request status to show to the user
val networkState: LiveData<NetworkState>,
// represents the refresh status to show to the user. Separate from networkState, this
// value is importantly only when refresh is requested.
val refreshState: LiveData<NetworkState>,
// refreshes the whole data and fetches it from scratch.
val refresh: () -> Unit,
// retries any failed requests.
val retry: () -> Unit)
NetworkState.kt
enum class Status {
RUNNING,
SUCCESS,
FAILED
}
@Suppress("DataClassPrivateConstructor")
data class NetworkState private constructor(
val status: Status,
val msg: String? = null) {
companion object {
val LOADED = NetworkState(Status.SUCCESS)
val LOADING = NetworkState(Status.RUNNING)
fun error(msg: String?) = NetworkState(Status.FAILED, msg)
}
}
RedditPostRepository.kt
interface RedditPostRepository {
fun postsOfSubreddit(subReddit: String, pageSize: Int): Listing<RedditPost>
enum class Type {
IN_MEMORY_BY_ITEM,
IN_MEMORY_BY_PAGE,
DB
}
}
SubRedditViewModel.k
观察来自 RedditPostRepository.kt
的 Listing<RedditPost>
通知 UI 有关状态和数据,UI 相应地更新视图。要了解有关 NetworkState
如何从 数据源传播到 ViewModel 的更多信息,请查看 PageKeyedSubredditDataSource.kt and SubRedditDataSourceFactory.kt
这是@yigit 对 Google 示例的 Link。
主要思想是保留对数据源工厂创建的数据源的引用。
下面是来自 https://github.com/googlesamples/android-architecture-components/tree/master/PagingWithNetworkSample
的代码片段
class SubRedditDataSourceFactory(
private val redditApi: RedditApi,
private val subredditName: String,
private val retryExecutor: Executor) : DataSource.Factory<String,
RedditPost>() {
val sourceLiveData = MutableLiveData<PageKeyedSubredditDataSource>()
override fun create(): DataSource<String, RedditPost> {
val source = PageKeyedSubredditDataSource(redditApi, subredditName,
retryExecutor)
sourceLiveData.postValue(source)
return source
}
}
在此代码示例中,'sourceLiveData' 发布活动数据源。
反过来,数据源可以向视图公开所有相关信息,如网络状态和指示器(如果首次加载已完成)
为了添加刷新行为,使用 SwipeRefreshLayout 包裹回收器视图,如下所示。现在您可以访问数据源,您可以使它无效并且视图将被刷新
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Paging Library 太棒了。但我发现缺少此功能:
- 加载数据时调度视图。假设我正在扩展
PageKeyedDataSource
:调用loadInitial
时顶部的视图,调用loadAfter
时列表底部的视图。当callback
被调用时,视图应该消失。 - 出现错误时调度视图。
- 滑动刷新
因为现在这是不可能的,有谁知道使用 PagingLibrary 的方法吗?至少一种在同一个列表中使用不同视图的方法。
有一个很好的示例@yigit 与 Paging 库相关,它还展示了如何在 Recyclerview 中处理进度条、错误和重试。所以基本上他正在创建一个 Listing.kt
数据 class,它由 NetworkState
和 PagedList
组成,如下所示
Listing.kt
data class Listing<T>(
// the LiveData of paged lists for the UI to observe
val pagedList: LiveData<PagedList<T>>,
// represents the network request status to show to the user
val networkState: LiveData<NetworkState>,
// represents the refresh status to show to the user. Separate from networkState, this
// value is importantly only when refresh is requested.
val refreshState: LiveData<NetworkState>,
// refreshes the whole data and fetches it from scratch.
val refresh: () -> Unit,
// retries any failed requests.
val retry: () -> Unit)
NetworkState.kt
enum class Status {
RUNNING,
SUCCESS,
FAILED
}
@Suppress("DataClassPrivateConstructor")
data class NetworkState private constructor(
val status: Status,
val msg: String? = null) {
companion object {
val LOADED = NetworkState(Status.SUCCESS)
val LOADING = NetworkState(Status.RUNNING)
fun error(msg: String?) = NetworkState(Status.FAILED, msg)
}
}
RedditPostRepository.kt
interface RedditPostRepository {
fun postsOfSubreddit(subReddit: String, pageSize: Int): Listing<RedditPost>
enum class Type {
IN_MEMORY_BY_ITEM,
IN_MEMORY_BY_PAGE,
DB
}
}
SubRedditViewModel.k
观察来自 RedditPostRepository.kt
的 Listing<RedditPost>
通知 UI 有关状态和数据,UI 相应地更新视图。要了解有关 NetworkState
如何从 数据源传播到 ViewModel 的更多信息,请查看 PageKeyedSubredditDataSource.kt and SubRedditDataSourceFactory.kt
这是@yigit 对 Google 示例的 Link。
主要思想是保留对数据源工厂创建的数据源的引用。 下面是来自 https://github.com/googlesamples/android-architecture-components/tree/master/PagingWithNetworkSample
的代码片段class SubRedditDataSourceFactory(
private val redditApi: RedditApi,
private val subredditName: String,
private val retryExecutor: Executor) : DataSource.Factory<String,
RedditPost>() {
val sourceLiveData = MutableLiveData<PageKeyedSubredditDataSource>()
override fun create(): DataSource<String, RedditPost> {
val source = PageKeyedSubredditDataSource(redditApi, subredditName,
retryExecutor)
sourceLiveData.postValue(source)
return source
}
}
在此代码示例中,'sourceLiveData' 发布活动数据源。 反过来,数据源可以向视图公开所有相关信息,如网络状态和指示器(如果首次加载已完成)
为了添加刷新行为,使用 SwipeRefreshLayout 包裹回收器视图,如下所示。现在您可以访问数据源,您可以使它无效并且视图将被刷新
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>