我在使用分页3库时遇到问题。我使用改装来获取数据,并使用分页3来显示它,但现在发生的情况是,当我第一次打开我的片段时,片段上什么都不显示,但当我关闭这个片段并再次打开它时,数据会显示出来。发生了什么?
我使用导航从一个片段导航到另一个片段。我在这个项目中使用Koin作为依赖项。
它现在所做的不是导航到片段然后加载数据,而是用Paging 3加载数据,然后导航到片段并显示数据(这只有在打开这个片段一次并关闭它之后才会发生(
我该怎么解决这个问题?我不知道为什么第一次不显示数据,然后我也不知道为什么它在导航前加载数据,而不是在使用库导航后加载ind数据。
我的代码:
服务:
interface APICalls{
@GET(MYAPIURL)
suspend fun getData(
@Header("Auth") auth : String,
@Query("pageSize") pageSize:Int
):Response<ResponseData>
}
型号:
data class ResponseData(
@SerializedName("listData") val listData:MutableList<DataAPI>,
@SerializedName("pageSize") val pageSize : Int
):Serializable
data class DataAPI(
@SerializedName("id") val id:Int,
@SerializedName("data")val data: String
): Serializable
结果包装器:
class Result<out T:Any>{
data class Success<T:Any>(val value: T): Result<T>()
data class Failure(val message:String, val errorCode:Int?):Result<Nothing>()
}
寻呼来源:
val responseData = mutableListOf<DataAPI>()
class DataAPIPagingSource(private val token:String,private val apiCalls:APICalls) : PagingSource<Int,DataAPI>{
override fun getRefreshKey(...):Int?{
return null
}
override suspend fun load(params : LoadParams<Int>):LoadResult<Int,DataAPI>{
return try{
val currentPage = params.key ?: 1
val response = apiCalls.getData(token)
response.body()?.let{
Result.Success(it)
}?: run{
Result.Failure(response.message(),response.code())
}
val data = response.body()?.listData ?: emptyList()
responseData.addAll(data)
LoadResult.Page(responseData,if(currentPage ==1) null else -1),currentPage.plus(1)
}catch(e:Exception){
LoadResult.Error(e)
}
}
}
视图模型:
class DataViewModel(private val apiCalls:APICalls): ViewModel {
//I get this token in shared preference
val token = .....
val mydata = getDataList()
.map{pagingData -> pagingData.map{DataModel.DataItem(it)}}
private fun getDataList(){
return Pager(PagingConfig(25)){
DataAPIPagingSource(token,apiCalls)
}.flow.cachedIn(viewModelScope)
}
}
sealed class DataModel{
data class DataItem(val dataitem: DataAPI) : DataModel()
}
private val DataModel.DataItem.identificator : Int
get() = this.dataItem.id
片段:
class MyFragment : Fragment(){
private val myAdapter : DataAdapter by lazy{
DataAdapter()
}
private val viewModelFactory : ViewModelFactory by inject()
private val dataViewModel : DataViewModel by navGraphViewModels(R.id.nav_graph){viewModelFactory}
override fun onViewCreated(...){
super.onViewCreated(...)
binding.myRecyclerView.apply{
adapter = myAdapter
layoutManager = StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL)
setHasFixedSize(true)
}
lyfecycleScope.launch{
dataViewModel.mydata.collect{myAdapter.submitData(it)}
}
}
}
适配器:
class DataAdapter : PagingDataAdapter<DataModel,RecyclerView.ViewHolder>(DataModelComparator){
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position:Int){
val dataModel : DataModel? = getItem(position)
dataModel.let{
when(dataModel){
is DataModel.DataItem ->{
val viewHolder = holder as DataItemViewHolder
viewHolder.binding.textview1.text = dataModel.dataitem.data
}
}
}
}
override fun getItemViewType(position:Int):Int{
return when(getItem(position)){
is DataModel.DataItem -> R.layout.item_data
null -> throw UnsupportedOperationException("Unknown view")
}
}
override fun onCreateViewHolder(...){
return when(viewType){
R.layout.item_data ->{
DataItemViewHolder(ItemDataBinding.inflate(...))
}
}
}
class DataItemViewHolder(val binding: DataItemBinding): RecyclerView.ViewHolder(binding.root)
companion object {
val DataModelComparator = object : DiffUtil.ItemCallback<DataModel>() {
override fun areItemsTheSame(oldItem: Movie, newItem: Movie): Boolean {
return oldItem.dataitem.id == newItem.dataitem.id
}
override fun areContentsTheSame(oldItem: DataModel, newItem: DataModel): Boolean {
return oldItem == newItem
}
}
}
}
删除setHasFixedSize(true(对我有效。