'How to change recycler view data according to TabItem

My app makes a call to an API that returns an "x" number of posts. In the following example two posts are returned, 001 and 002, but this can change and return ten posts. I already created the code to add a tab item for each post.

How do I link the clicks to each list and put it in the adapter of the recycler view according to the click on the tab?

My code:

val arrayList : ArrayList<List<Abastecimento>> = arrayListOf()
for (posto in listaIDPostos){
    var list : MutableList<Abastecimento> = mutableListOf()
    for (abastecimento in lista){
        if (abastecimento.station == posto){
            list.add(abastecimento)
        }
    }
    arrayList.add(list)
    println(arrayList)
}

for(i in listaIDPostos){
    select_bar.addTab(select_bar.newTab().setText(i))
}

select_bar.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
    override fun onTabSelected(tab: TabLayout.Tab?) {
        when (tab?.position) {
            //NEED SOLUTION HERE
            0 -> {
                recycler_view_abastecimentos.adapter =
                    ListaAbastecimentoAdapter(applicationContext,
                        arrayList[0] as MutableList<Abastecimento>
                    )
            }
            1 -> {
                recycler_view_abastecimentos.adapter =
                    ListaAbastecimentoAdapter(applicationContext, arrayList[1] as MutableList<Abastecimento>)
            }
        }
    }


Solution 1:[1]

I got a solution

 select_bar.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        override fun onTabSelected(tab: TabLayout.Tab?) {
            when (tab?.position) {
                //NEED SOLUTION HERE
                  tab?.position ->  recycler_view_abastecimentos.adapter =
                        ListaAbastecimentoAdapter(applicationContext,
                            arrayList[tab!!.position] as MutableList<Abastecimento>
                        )

            }
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ldPr