如何刷新 paging 3 库中的页码
how can I refresh the page number in paging 3 library
我正在使用分页 3 在回收站视图中加载一些数据。我从服务器获取数据,然后将它们存储在我的本地数据库中。
所以这是我的 DAO 接口:
@Dao
interface TicketDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(model: TicketListModel)
@Query("SELECT * FROM ticket_list ORDER BY lastModifyDate DESC , lastModifyTime DESC")
fun getTickets(): PagingSource<Int, TicketListModel>
@Query("delete from ticket_list")
fun deleteAll()
}
我假设这个界面是我的数据源class。因为我也从服务器获取数据,所以我使用了这样的远程中介(顺便说一下,我使用 volley 从服务器获取数据):
@ExperimentalPagingApi
class TicketRemoteMediator(val ticketDatabase: TicketDAO, val context: Context) :
RemoteMediator<Int, TicketListModel>() {
private val scope = CoroutineScope(Dispatchers.Default)
private var page = 0
private var reachedEnd = false
override suspend fun load(
loadType: LoadType,
state: PagingState<Int, TicketListModel>
): MediatorResult {
return try {
fetchData(page = page)
page += 1
Log.i("Log","page is $page")
MediatorResult.Success(endOfPaginationReached = reachedEnd)
} catch (e: Exception) {
page -= 1
MediatorResult.Error(e)
}
}
private fun fetchData(page: Int) {
val request = object : JsonObjectRequest(
Method.GET, Address().getTicketAPI(page), null, Response.Listener {
reachedEnd = true
val info = it.getJSONArray("results")
for (i in 0 until info.length()) {
val data = info.getJSONObject(i)
scope.launch {
async(Dispatchers.Default, CoroutineStart.DEFAULT, block = {
ticketDatabase.insert(
TicketListModel(
subject = data.getString("subject"),
status = data.getString("status"),
)
reachedEnd = false
}).await()
}
}
}, Response.ErrorListener {
this.page -= 1
reachedEnd = false
try {
Log.i("Log", "error in ticket remoteMediator ${String(it.networkResponse.data,Charsets.UTF_8)}")
}catch (e:Exception){
Log.i("Log", "error in ticket mediator $it")
}
}
) {
@Throws(AuthFailureError::class)
override fun getHeaders(): MutableMap<String, String> {
val token = HashMap<String, String>()
token["Authorization"] =
"Bearer ${Config().accessToken}"
return token
}
}
request.retryPolicy = DefaultRetryPolicy(10000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
val queue = Volley.newRequestQueue(context)
queue.add(request)
}
}
并使用我的视图模型 class 像这样使用数据:
val isLoading = MutableLiveData<Boolean>()
@ExperimentalPagingApi
val tickets= Pager(
config = PagingConfig(pageSize =5),
remoteMediator = TicketRemoteMediator(ticketDatabase = database, context = context)
) {
database.getTickets()
}.liveData.cachedIn(viewModelScope).also {
isLoading.value = false
}
我的片段中有这段代码:
viewModel.tickets.observe(viewLifecycleOwner, Observer {
viewLifecycleOwner.lifecycleScope.launch {
adapter.submitData(it)
}
})
回收器视图适配器的哪一部分是:
class TicketListAdapter : PagingDataAdapter<TicketListModel, TicketListAdapter.ViewHolder>(
diffCallback = object : DiffUtil.ItemCallback<TicketListModel>() {
override fun areItemsTheSame(oldItem: TicketListModel, newItem: TicketListModel): Boolean {
return oldItem.ticket_id == newItem.ticket_id
}
override fun areContentsTheSame(
oldItem: TicketListModel,
newItem: TicketListModel
): Boolean {
return oldItem == newItem
}
}
) {
...
}
现在,我有两个问题:
首先:
有一次我的用户想要刷新列表。使用滑动刷新。
所以我为此设置了这行代码但没有任何反应:
binding.refresh.setOnRefreshListener {
database.getTickets().invalidate()
}
其次:
来自服务器的分页库请求很多,即使用户还没有滚动。事实上,当我登录时,我看到有 20 多个请求被发送到服务器,而在 1 或 2 个请求之后,所有信息都可以从服务器获取。
如何解决这些问题?
刷新很简单,你只需要在你的适配器上调用refresh()
binding.refresh.setOnRefreshListener{
pagingAdapter.refresh()
}
至于有多个请求,这可能与您给PagingConfig
的pageSize
有关。如果您查看其中的其他参数,您会发现 initialLoadSize
的默认值是 pageSize
的三倍。这意味着它会在它满意之前尝试加载 pageSize
*3 项。 Address().getTicketAPI(page)
返回了多少项目
更新:
我刚刚注意到您的 load
方法总是假定我们正在尝试加载下一页,而实际上它也被调用以刷新和加载前一页。 loadType
参数决定了我们要加载的方向。
这是您的加载方法的示例
val nextPage = when (loadType) {
LoadType.REFRESH -> 0 // we're refreshing so just reset the page
LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true) // shouldn't need to prepend
LoadType.APPEND -> {
// work out the next page number
page + 1
}
return try {
fetchData(page = nextPage)
Log.i("Log","page is $nextPage")
MediatorResult.Success(endOfPaginationReached = reachedEnd)
} catch (e: Exception) {
MediatorResult.Error(e)
}
我正在使用分页 3 在回收站视图中加载一些数据。我从服务器获取数据,然后将它们存储在我的本地数据库中。 所以这是我的 DAO 接口:
@Dao
interface TicketDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(model: TicketListModel)
@Query("SELECT * FROM ticket_list ORDER BY lastModifyDate DESC , lastModifyTime DESC")
fun getTickets(): PagingSource<Int, TicketListModel>
@Query("delete from ticket_list")
fun deleteAll()
}
我假设这个界面是我的数据源class。因为我也从服务器获取数据,所以我使用了这样的远程中介(顺便说一下,我使用 volley 从服务器获取数据):
@ExperimentalPagingApi
class TicketRemoteMediator(val ticketDatabase: TicketDAO, val context: Context) :
RemoteMediator<Int, TicketListModel>() {
private val scope = CoroutineScope(Dispatchers.Default)
private var page = 0
private var reachedEnd = false
override suspend fun load(
loadType: LoadType,
state: PagingState<Int, TicketListModel>
): MediatorResult {
return try {
fetchData(page = page)
page += 1
Log.i("Log","page is $page")
MediatorResult.Success(endOfPaginationReached = reachedEnd)
} catch (e: Exception) {
page -= 1
MediatorResult.Error(e)
}
}
private fun fetchData(page: Int) {
val request = object : JsonObjectRequest(
Method.GET, Address().getTicketAPI(page), null, Response.Listener {
reachedEnd = true
val info = it.getJSONArray("results")
for (i in 0 until info.length()) {
val data = info.getJSONObject(i)
scope.launch {
async(Dispatchers.Default, CoroutineStart.DEFAULT, block = {
ticketDatabase.insert(
TicketListModel(
subject = data.getString("subject"),
status = data.getString("status"),
)
reachedEnd = false
}).await()
}
}
}, Response.ErrorListener {
this.page -= 1
reachedEnd = false
try {
Log.i("Log", "error in ticket remoteMediator ${String(it.networkResponse.data,Charsets.UTF_8)}")
}catch (e:Exception){
Log.i("Log", "error in ticket mediator $it")
}
}
) {
@Throws(AuthFailureError::class)
override fun getHeaders(): MutableMap<String, String> {
val token = HashMap<String, String>()
token["Authorization"] =
"Bearer ${Config().accessToken}"
return token
}
}
request.retryPolicy = DefaultRetryPolicy(10000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
val queue = Volley.newRequestQueue(context)
queue.add(request)
}
}
并使用我的视图模型 class 像这样使用数据:
val isLoading = MutableLiveData<Boolean>()
@ExperimentalPagingApi
val tickets= Pager(
config = PagingConfig(pageSize =5),
remoteMediator = TicketRemoteMediator(ticketDatabase = database, context = context)
) {
database.getTickets()
}.liveData.cachedIn(viewModelScope).also {
isLoading.value = false
}
我的片段中有这段代码:
viewModel.tickets.observe(viewLifecycleOwner, Observer {
viewLifecycleOwner.lifecycleScope.launch {
adapter.submitData(it)
}
})
回收器视图适配器的哪一部分是:
class TicketListAdapter : PagingDataAdapter<TicketListModel, TicketListAdapter.ViewHolder>(
diffCallback = object : DiffUtil.ItemCallback<TicketListModel>() {
override fun areItemsTheSame(oldItem: TicketListModel, newItem: TicketListModel): Boolean {
return oldItem.ticket_id == newItem.ticket_id
}
override fun areContentsTheSame(
oldItem: TicketListModel,
newItem: TicketListModel
): Boolean {
return oldItem == newItem
}
}
) {
...
}
现在,我有两个问题:
首先: 有一次我的用户想要刷新列表。使用滑动刷新。 所以我为此设置了这行代码但没有任何反应:
binding.refresh.setOnRefreshListener {
database.getTickets().invalidate()
}
其次: 来自服务器的分页库请求很多,即使用户还没有滚动。事实上,当我登录时,我看到有 20 多个请求被发送到服务器,而在 1 或 2 个请求之后,所有信息都可以从服务器获取。
如何解决这些问题?
刷新很简单,你只需要在你的适配器上调用refresh()
binding.refresh.setOnRefreshListener{
pagingAdapter.refresh()
}
至于有多个请求,这可能与您给PagingConfig
的pageSize
有关。如果您查看其中的其他参数,您会发现 initialLoadSize
的默认值是 pageSize
的三倍。这意味着它会在它满意之前尝试加载 pageSize
*3 项。 Address().getTicketAPI(page)
更新:
我刚刚注意到您的 load
方法总是假定我们正在尝试加载下一页,而实际上它也被调用以刷新和加载前一页。 loadType
参数决定了我们要加载的方向。
这是您的加载方法的示例
val nextPage = when (loadType) {
LoadType.REFRESH -> 0 // we're refreshing so just reset the page
LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true) // shouldn't need to prepend
LoadType.APPEND -> {
// work out the next page number
page + 1
}
return try {
fetchData(page = nextPage)
Log.i("Log","page is $nextPage")
MediatorResult.Success(endOfPaginationReached = reachedEnd)
} catch (e: Exception) {
MediatorResult.Error(e)
}