Bali Codes
Bali Codes

Reputation: 721

Kotlin : Pagination Recyclerview Not Working After Swipe Refresh Layout

Recyclerview pagination works well, but where i do SwipeRefreshLayout, the pagination became broke and doesn't work as expected.

This OnScrollListener of Recyclerview

rv_spesialisasi.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val linearLayoutManager = recyclerView
                    ?.layoutManager as LinearLayoutManager?
                if (!loading && linearLayoutManager!!.itemCount <= linearLayoutManager.findLastVisibleItemPosition() + 2) {
                    loading = true
                    refresing(true)
                    viewModel.loadIconSpesialisasi(currentpage)
                }
            }
        })

and this is for refreshing

private fun setRefreshing() {
        sr_spesialisasi.setOnRefreshListener {
            currentpage = 0
            loading = false
            listDataGlobal.clear()
            adapter.clearData()

            viewModel.loadIconSpesialisasi(currentpage)
        }
    }

I have tried in java, it's works well, but in kotlin doesnt work. Please help me to solve this problem.

Upvotes: 1

Views: 798

Answers (1)

Mohan Sai Manthri
Mohan Sai Manthri

Reputation: 3189

May I know what you're executing under refresing (true) method.

If you're using or showing swipe layout's loader as loading indicator & for that If you're calling swipeLayout.isRefreshing = true this will show loader but also triggers the onRefreshListener(), In this case it will call

sr_spesialisasi.setOnRefreshListener {
        currentpage = 0
        loading = false
        listDataGlobal.clear()
        adapter.clearData()

        viewModel.loadIconSpesialisasi(currentpage)
    }

this method, which again reloads to the first page.

To make it work, use any boolean like this.isRealRefreshIsOn = false when you wanna refresh make this to true.

 sr_spesialisasi.setOnRefreshListener {
       if(isRealRefreshIsOn){
        currentpage = 0
        loading = false
        listDataGlobal.clear()
        adapter.clearData()

        viewModel.loadIconSpesialisasi(currentpage)}
    }

Upvotes: 0

Related Questions