I need the TabLayout to have an "x" number of tabs, which can vary according to the user’s data that is already in the database
My xml:
enter code here
<com.google.android.material.tabs.TabLayout
android:id="@+id/select_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabContentStart="32dp"
app:tabMode="scrollable">
<com.google.android.material.tabs.TabItem
android:id="@+id/posto1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Posto Shell" />
<com.google.android.material.tabs.TabItem
android:id="@id/posto2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Posto Guanabara" />
My code:
select_bar.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
when (tab?.position) {
0 -> {
recycler_view_abastecimentos.adapter =
ListaAbastecimentoAdapter(applicationContext, lista)
}
1 -> {
recycler_view_abastecimentos.adapter =
ListaAbastecimentoAdapter(applicationContext, lista)
}
}
}
3
Answers
You can do so by:
See the documentation here: https://developer.android.com/reference/com/google/android/material/tabs/TabLayout
You can achieve this by making a list. From your list, you can dynamically create your tabs.
Example: