nation wantstoknow
nation wantstoknow

Reputation: 33

Handler.postdelay() not working inside recycler view on scroll state change

I'm basically trying to execute a method in my RecyclerView inside on scroll state change here I'm using a Handler thread to execute a method after some delay but the method is not getting executed at all but however if I put that method outside the handler thread it's getting executed successfully so my question is why am I unable to run Handler thread inside RecyclerView onscroll here is my code please take a look.

rvsongs!!.addOnScrollListener(object: RecyclerView.OnScrollListener(){
                override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if(newState== SCROLL_STATE_IDLE) {
                    val timerHandler = Handler()
                    var updater:Runnable?=null
                    updater = object:Runnable {
                        override fun run() {
                            rvsongs!!.setIndexBarVisibility(false)//this method is not getting executed however when put outside handler thread it's executed without any problem
                        }
                    }
                    timerHandler.postDelayed(updater,100)
                }
                else
                {
                    rvsongs!!.setIndexBarVisibility(true)
                }
            }
        })

Upvotes: 3

Views: 669

Answers (1)

Ferran
Ferran

Reputation: 1448

The issue is that you are calling setIndexBarVisibilty after the complete onScrollStateChanged is processed. In other words, the recyclerView is rendered before setIndexBatVisibility.

To solve this, just call invalidate after setIndexBarVisibility

       ...
       var updater:Runnable?=null
        updater = object:Runnable {
            override fun run() {
                rvsongs!!.setIndexBarVisibility(false)
                rvsongs.invalidate()
            }
        }
        ...

Upvotes: 1

Related Questions