Android Paging3 列表项跳转到屏幕开头并无限循环
Android Paging3 list items jumps to beginning of the screen and infinite loop
我的应用程序中有一个 recyclerview,我想让它与 Android paging3 library.Its 一起工作,但不是我想要的方式。在这里,当我打开列表时,我在 PagingConfig.pageSize 中定义的数据数量在顶部创建。当我继续滚动时,它永远不会像无限循环一样停止。但是当我像这样向下滚动它的工作正常时;
如您所见,向上滚动始终处于启用状态。
我不知道我的代码有什么问题;
我的适配器;
package com.tolgahantutar.bexworkfloww.ui.addressbook
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.Navigation
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.tolgahantutar.bexworkfloww.R
import com.tolgahantutar.bexworkfloww.data.models.userget.UserGetValue
import kotlinx.android.synthetic.main.contacts_layout.view.*
class UserAdapter() :
PagingDataAdapter<UserGetValue, UserAdapter.UserViewHolder>(DiffCallBack()) {
class DiffCallBack : DiffUtil.ItemCallback<UserGetValue>(){
override fun areItemsTheSame(oldItem: UserGetValue, newItem: UserGetValue): Boolean {
return oldItem.name == newItem.name
}
override fun areContentsTheSame(oldItem: UserGetValue, newItem: UserGetValue): Boolean {
return oldItem.name == newItem.name
}
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.itemView.contac_name_textview.text =getItem(position)!!.name
holder.itemView.user_profile_text_left_drawable.setText("${(getItem(position)!!.name.substringBefore(" ")).subSequence(0,1)}"+"${(getItem(position)!!.name.substringAfter(" ").subSequence(0,1))}")
holder.itemView.contact_info_text.text = getItem(position)!!.title
var getContactValue = getItem(position)!!
holder.itemView.setOnClickListener {
val action = AdressBookFragmentDirections.actionUserDetail(getItem(position)!!)
Navigation.findNavController(it).navigate(action)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
return UserViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.contacts_layout, parent, false)
)
}
class UserViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
}
}
PagingSource;
import androidx.paging.PagingSource
import com.tolgahantutar.bexworkfloww.data.models.userget.UserGetValue
import com.tolgahantutar.bexworkfloww.data.network.apis.WorkFlowServicesApi
class UserRemotePagingSource(private val api : WorkFlowServicesApi): PagingSource<Int,UserGetValue>(){
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, UserGetValue> {
return try {
val page = params.key?: 1
val response = api.userGet(2,page,2)
LoadResult.Page(
data = response.body()!!.userGetValue,
prevKey = page-1,
nextKey = page+1
)
}catch (e: Exception){
LoadResult.Error(e)
}
}
}
视图模型;
fun usersPagingFlow(): Flow<PagingData<UserGetValue>>{
return Pager(
config = PagingConfig(
pageSize = 2,
prefetchDistance = 2,
enablePlaceholders = true,
initialLoadSize = 2*3
)
){
UserRemotePagingSource(api)
}.flow
}
和片段;
recycler_view_contacts.setHasFixedSize(true)
recycler_view_contacts.adapter = pagingDataAdapter
recycler_view_contacts.layoutManager = LinearLayoutManager(requireContext())
lifecycleScope.launch {
addressBookViewModel.usersPagingFlow().collectLatest { pagingData ->
pagingDataAdapter.submitData(pagingData)
}
}
在您的 PagingSource 实现中,您似乎需要检查您的键何时为 1,然后将 prevKey
设置为 null
以标记列表的结尾。
当你给你的 api 一个负数时,它看起来像是发生了什么,它只是再次加载第一页。
我的应用程序中有一个 recyclerview,我想让它与 Android paging3 library.Its 一起工作,但不是我想要的方式。在这里,当我打开列表时,我在 PagingConfig.pageSize 中定义的数据数量在顶部创建。当我继续滚动时,它永远不会像无限循环一样停止。但是当我像这样向下滚动它的工作正常时;
如您所见,向上滚动始终处于启用状态。
我不知道我的代码有什么问题;
我的适配器;
package com.tolgahantutar.bexworkfloww.ui.addressbook
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.Navigation
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.tolgahantutar.bexworkfloww.R
import com.tolgahantutar.bexworkfloww.data.models.userget.UserGetValue
import kotlinx.android.synthetic.main.contacts_layout.view.*
class UserAdapter() :
PagingDataAdapter<UserGetValue, UserAdapter.UserViewHolder>(DiffCallBack()) {
class DiffCallBack : DiffUtil.ItemCallback<UserGetValue>(){
override fun areItemsTheSame(oldItem: UserGetValue, newItem: UserGetValue): Boolean {
return oldItem.name == newItem.name
}
override fun areContentsTheSame(oldItem: UserGetValue, newItem: UserGetValue): Boolean {
return oldItem.name == newItem.name
}
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.itemView.contac_name_textview.text =getItem(position)!!.name
holder.itemView.user_profile_text_left_drawable.setText("${(getItem(position)!!.name.substringBefore(" ")).subSequence(0,1)}"+"${(getItem(position)!!.name.substringAfter(" ").subSequence(0,1))}")
holder.itemView.contact_info_text.text = getItem(position)!!.title
var getContactValue = getItem(position)!!
holder.itemView.setOnClickListener {
val action = AdressBookFragmentDirections.actionUserDetail(getItem(position)!!)
Navigation.findNavController(it).navigate(action)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
return UserViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.contacts_layout, parent, false)
)
}
class UserViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
}
}
PagingSource;
import androidx.paging.PagingSource
import com.tolgahantutar.bexworkfloww.data.models.userget.UserGetValue
import com.tolgahantutar.bexworkfloww.data.network.apis.WorkFlowServicesApi
class UserRemotePagingSource(private val api : WorkFlowServicesApi): PagingSource<Int,UserGetValue>(){
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, UserGetValue> {
return try {
val page = params.key?: 1
val response = api.userGet(2,page,2)
LoadResult.Page(
data = response.body()!!.userGetValue,
prevKey = page-1,
nextKey = page+1
)
}catch (e: Exception){
LoadResult.Error(e)
}
}
}
视图模型;
fun usersPagingFlow(): Flow<PagingData<UserGetValue>>{
return Pager(
config = PagingConfig(
pageSize = 2,
prefetchDistance = 2,
enablePlaceholders = true,
initialLoadSize = 2*3
)
){
UserRemotePagingSource(api)
}.flow
}
和片段;
recycler_view_contacts.setHasFixedSize(true)
recycler_view_contacts.adapter = pagingDataAdapter
recycler_view_contacts.layoutManager = LinearLayoutManager(requireContext())
lifecycleScope.launch {
addressBookViewModel.usersPagingFlow().collectLatest { pagingData ->
pagingDataAdapter.submitData(pagingData)
}
}
在您的 PagingSource 实现中,您似乎需要检查您的键何时为 1,然后将 prevKey
设置为 null
以标记列表的结尾。
当你给你的 api 一个负数时,它看起来像是发生了什么,它只是再次加载第一页。