kotlin 分页3 Android不显示数据

oknwwptz  于 2023-06-24  发布在  Kotlin
关注(0)|答案(1)|浏览(172)

我有一个问题,这是几天我必须提交这个项目,我不知道为什么数据不显示在recyclerview,当我使用分页3.但可以肯定的是,当我点击API时,数据被成功检索,但由于某种原因,数据无法显示在recyclerview上。

StoryPagingSource.kt

class StoryPagingSource(
        private val apiService: APIService,
        private val dataStore:DataStoreRepository
    ):
    PagingSource<Int, getStoryResult>() {
    private companion object {
        const val INITIAL_PAGE_INDEX = 1
    }
    override fun getRefreshKey(state: PagingState<Int,getStoryResult>): Int? {
        return state.anchorPosition?.let { anchorPosition ->
            val anchorPage = state.closestPageToPosition(anchorPosition)
            anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
        }
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int,getStoryResult> {
        return try {
            val position = params.key ?: INITIAL_PAGE_INDEX
            val responseData = apiService.getAllStories(
                Bearer = "Bearer ${dataStore.getToken(AddStoryViewModel.TOKEN_KEY).toString()}",
                location = 0,
                page = params.loadSize
            )
            LoadResult.Page(
                data = responseData.listStory,
                prevKey = if (position == INITIAL_PAGE_INDEX) null else position - 1,
                nextKey = if (responseData.listStory.isEmpty()) null else position + 1
            )
        } catch (e: Exception) {
            return LoadResult.Error(e)
        }
    }

StoryRepositoryImpl.kt

class StoryRepositoryImpl @Inject constructor(
    private val apiService: APIService,
    private val dataStoreRepository: DataStoreRepository
):StoryRepository{
    override fun getStory(): LiveData<PagingData<getStoryResult>> {
        val pagingSource = StoryPagingSource(
            dataStore = dataStoreRepository,
            apiService = apiService
        )
        return Pager(
            config = PagingConfig(pageSize = 5),
            pagingSourceFactory = { pagingSource }
        ).liveData
    }
}

StoryViewModel.kt

val story:LiveData<PagingData<getStoryResult>> = storyRepository.getStory().cachedIn(viewModelScope)

适配器:

class StoryAdapter(val context: Context):PagingDataAdapter<getStoryResult,StoryAdapter.ViewHolder>(
    DIFF_CALLBACK){
    inner class ViewHolder(private val binding:ItemNotesBinding):RecyclerView.ViewHolder(binding.root){
        fun bind(getStoryResult: getStoryResult){
            with(binding){
                description.text = getStoryResult.description
                Glide.with(context).load(getStoryResult.photo).into(photo)
                detailStory.setOnClickListener {view->
                    val toDetailStoryBinding = HomeFragmentDirections.actionHomeFragmentToDetailStory2()
                    toDetailStoryBinding.id = getStoryResult.id
                    view.findNavController().navigate(toDetailStoryBinding)
                }
            }
        }

    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(ItemNotesBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }
    override fun getItemCount(): Int = 0

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        getItem(position)?.let { holder.bind(it) }
    }
    companion object {
        private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<getStoryResult>() {
            override fun areItemsTheSame(oldItem: getStoryResult, newItem: getStoryResult): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: getStoryResult, newItem: getStoryResult): Boolean {
                return oldItem.id == newItem.id
            }
        }
    }
}

HomeFragment.kt

private fun setAdapter(){
        val adapter = StoryAdapter(requireContext())
        binding.rvNotes.adapter = adapter
        storyViewModel.story.observe(viewLifecycleOwner){
            adapter.submitData(lifecycle,it)
        }
 }

Thank you guys:)
我试着解决这个问题,但我不能。我希望数据可以显示到recyclerview中

uidvcgyl

uidvcgyl1#

OMG我太笨了,问题已经解决了,只要删除适配器中的getitemcount函数:)

相关问题