Android Kotlin change ImageView image source in RecyclerView

588 views Asked by At

I am struggling with a problem. I have an activity that has a RecyclerView with images. The RecyclerView is filled throught adapter, and that's works fine, and I got his starting screen.

start screen

After this I want to change this images in a loop with a pause of 2 seconds randomly. That is where I need help, because what I already tried, showed the end screen immediately, there is time between 2 image changes, and this is what shows.

result screen

I am trying this to achieve in the onStart function calling hideElements() function. Here is my hideElements() function:

private fun hideElements()
    {
        if(recyclerView.adapter?.itemCount == 0)
        {
            return
        }

        var hideOrdder: ArrayList<Int> = ArrayList<Int>()

        for (i in 0 until recyclerView.adapter?.itemCount!!)
        {
            hideOrdder.add(i)
        }

        hideOrdder.shuffle()

        for(i in hideOrdder)
        {
            _gridElementList[i].drawableId = R.drawable.kerojel

            handler = Handler()
            runnable = Runnable {
                adapter!!.notifyItemChanged(i)
            }

            runOnUiThread {
                handler!!.postDelayed(runnable!!, 10000)
            }
        }
    }

I tried also to create a thread, but I failed. thnx

2

There are 2 answers

0
Wasyster On
override fun onStart()
    {
        putElements()

        var hideOrderList: ArrayList<Int> = hideOrderList()

        object : CountDownTimer((settings.row * settings.column * settings.speed * 1000).toLong(), (settings.speed * 1000).toLong())
        {
            override fun onTick(miliSeconds: Long)
            {
                if (changeIndex < settings.row * settings.column)
                {
                    _gridElementList[hideOrderList[changeIndex]].drawableId = R.drawable.kerojel
                    adapter!!.notifyItemChanged(hideOrderList[changeIndex])

                    changeIndex++
                }
            }

            override fun onFinish()
            {
                Toast.makeText(this@GameActivity, "START", Toast.LENGTH_SHORT)
                     .show()
            }
        }.start()

        startCounter()

        super.onStart()
    }
0
SpiritCrusher On

You are changing whole dataset _gridElementList in one go inside loop . Thats not right. There can be multiple solution . You can use handler and a Timer. posting a solution with handler. its not elegant but it should do the job. I have set one delayed call with handler and setting subsequent call when it fires after 2 seconds.

val list = ArrayList<Int>()
val hideOrder: ArrayList<Int> = ArrayList<Int>()
val handler=Handler()
val runnable = Runnable {
    list[hideOrder[changeIndex]].drawableId=R.drawable.kerojel
    adapter?.notifyItemChanged(hideOrder[changeIndex])
    scheduleHideElements()
}
var changeIndex=-1
private fun scheduleHideElements() {
    if(changeIndex<hideOrder.size){
        changeIndex++
        handler.postDelayed(runnable,2000)
    }
}

you need to save hideOrder beforehand at class level . and this should work. its not the complete code but you get the idea . Do make sure you call handler.removeCallbacks(runnable) in onDestroy().