如何根据TabItem更改回收器视图数据



我的应用程序对API进行调用;x〃;职位数量。在下面的示例中,返回了两个post,001和002,但这可以更改并返回十个post。我已经创建了为每个帖子添加标签项的代码。

如何将点击链接到每个列表,并根据选项卡上的点击将其放入回收器视图的适配器中?

我的代码:

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>)
}
}
}

我得到了一个解决方案

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>
)
}
}

最新更新