Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Calling refresh() method twice, stops the pagination.

Aperta
#223 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
android, kotlin
Ambito
mobile

Direzione di ricerca

Inizia riproducendo il comportamento di doppio aggiornamento in VideosFragment usando il flusso pull-to-refresh fornito, quindi traccia adapter.refresh(), submitData() e loadStateFlow insieme a VideoPagingSource.kt e PreClassViewModel.kt. Il lavoro è completato quando la paginazione continua a funzionare dopo che refresh() viene chiamato due volte e lo stato di aggiornamento viene ancora cancellato correttamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I'm able to populate the list and pagination is working as expected. But when I call adapter.refresh() method twice by using pull to refresh, the pagination stops working.

Please refer the below video to get the better idea about the prolem.
https://www.dropbox.com/s/0k2g9mlktv5gee6/22-05-22-10-26-37.mp4?dl=0

VideoPagingSource.kt

class VideoPagingSource(
    private val apiInterface: ApiInterface,
    private val schoolId: String,
    private val ordering: String?,
    private val courseId: String?,
    private val moduleId: String?,
    private val searchText: String?
) : PagingSource<Int, Video>() {


    override fun getRefreshKey(state: PagingState<Int, Video>): Int? = null

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Video> {
        val pageNumber = params.key
        return try {
            val response = apiInterface.getVideos(schoolId, ordering, courseId, moduleId, searchText, pageNumber)
            val pagedResponse = response?.body()
            var nextPageNumber: Int? = null
            if (pagedResponse?.links?.next != null) {
                val uri = Uri.parse(pagedResponse.links.next)
                val nextPageQuery = uri.getQueryParameter("page")
                nextPageNumber = nextPageQuery?.toInt()
            }

            LoadResult.Page(
                data = response?.body()?.objects.orEmpty(),
                prevKey = null,
                nextKey = nextPageNumber
            )
        } catch (e: Exception) {
            LoadResult.Error(e)
        }
    }


}

PreClassViewModel.kt

class PreClassViewModel @Inject constructor(private val repository: PreClassRepository) :
    ViewModel() {

    private val queryParamLiveData = MutableLiveData<QueryParams>()

    val videosLiveData = queryParamLiveData.switchMap {

        repository.getVideos(
            schoolId = it.extraArgs[0],
            ordering = it.extraArgs[1],
            courseId = it.courseId,
            moduleId = it.chapterId,
            searchText = it.searchText
        ).cachedIn(viewModelScope)

    }


    fun setQueryParam(queryParams: QueryParams){
        queryParamLiveData.value = queryParams
    }

}

VideosFragment.kt

class VideosFragment : BaseFragment<FragmentVideosBinding>(FragmentVideosBinding::inflate),
    VideosPagingAdapter.VideoClickListener {

    private val viewModel by viewModels<PreClassViewModel>()
    private var chapter: ModulesItem? = null
    private var courseId: String? = null
    private var adapter: VideosPagingAdapter? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        chapter = Gson().fromJson(arguments?.getString(BUNDLE_CHAPTER), ModulesItem::class.java)
        courseId = arguments?.getString(BUNDLE_COURSE_ID)


        viewModel.setQueryParam(
            QueryParams(
                courseId = courseId,
                chapterId = chapter?.id.toString(),
                searchText = null,
                me?.academicProfile?.school?.id?.toString()!!,
                "sequence",
            )
        )

    }

    override fun initView() {
        getMainActivity().showBothNavigation()
        getMainActivity().showBackButton()

        adapter = VideosPagingAdapter(chapter?.imageUrl, this)
        binding.videoRV.adapter = adapter!!.withLoadStateFooter(footer = DefaultFooterAdapter())
        lifecycleScope.launch {
            adapter?.loadStateFlow?.collectLatest {

                binding.progressBar.isVisible = it.refresh is LoadState.Loading

                if (binding.swipeContainer.isRefreshing){
                    binding.swipeContainer.isRefreshing = it.refresh is LoadState.Loading
                }



            }
        }


    }

    override fun initListener() {
        binding.swipeContainer.setOnRefreshListener {
            adapter?.refresh()
        }
    }

    override fun addObserver() {

        viewModel.videosLiveData.observe(viewLifecycleOwner) {
            adapter?.submitData(viewLifecycleOwner.lifecycle, it)

        }


    }

    override var setSelection: String
        get() = Constants.FEATURE_LEARN
        set(value) {}

    override fun onVideoClick(video: Video) {

    }

}
Lingua principale
Kotlin
Stelle
508
Fork
268
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di android/codelab-android-paging

Tutte le issue di android/codelab-android-paging

Issue simili

Altre issue su Kotlin

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.