Usage
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
This page shows how to add RecyclerTabLayout (package com.irurueta.android.recyclertablayout) to a screen, supply
its tabs through an adapter, select tabs and keep it in sync with a ViewPager2. Every attribute and property
mentioned here is listed with its default value in the Configuration reference.
The Kotlin examples on this page are copied from DocumentationExamplesTest in the lib module’s unit tests,
which compiles and runs every one of them and fails when a snippet here no longer matches.
|
Adding the view to a layout
RecyclerTabLayout extends RecyclerView, so it is declared like any other view. Custom attributes use the app:
(res-auto) namespace:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.irurueta.android.recyclertablayout.RecyclerTabLayout
android:id="@+id/recycler_tab_layout"
android:layout_width="match_parent"
android:layout_height="48dp"
app:tabIndicatorColor="?attr/colorPrimary"
app:tabIndicatorHeight="2dp" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
The view sets up its own horizontal LinearLayoutManager when it is created. Do not replace the layout manager:
the indicator relies on it to find the selected tab.
Creating the view in code
RecyclerTabLayout has a @JvmOverloads constructor (context, attrs = null, defStyleAttr = 0), so it can also be
created programmatically. It then uses the default RecyclerTabLayout style:
val recyclerTabLayout = RecyclerTabLayout(context)
recyclerTabLayout.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
container.addView(recyclerTabLayout)
Defining tabs with an adapter
Tabs are the items of a regular RecyclerView.Adapter. Each tab can be any view: a TextView, an icon, or a
thumbnail of an image, which is where recycling pays off. The adapter below creates one TextView per visible tab and
reports clicks through a callback:
class TabViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
class TabAdapter(
private val titles: List<String>,
private val onTabClick: (position: Int) -> Unit
) : RecyclerView.Adapter<TabViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabViewHolder {
val density = parent.resources.displayMetrics.density
val textView = TextView(parent.context)
textView.gravity = Gravity.CENTER
textView.minWidth = (72 * density).toInt()
textView.layoutParams = RecyclerView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
return TabViewHolder(textView)
}
override fun onBindViewHolder(holder: TabViewHolder, position: Int) {
holder.textView.text = titles[position]
holder.textView.setOnClickListener {
val clickedPosition = holder.bindingAdapterPosition
if (clickedPosition != RecyclerView.NO_POSITION) {
onTabClick(clickedPosition)
}
}
}
override fun getItemCount(): Int = titles.size
}
-
Make the tab views' height
MATCH_PARENT. The indicator is drawn along the bottom edge of theRecyclerTabLayout, under the selected tab’s view. -
Give tab views a minimum width (72 dp here, like Material tabs) and a click listener, so that they are large enough to be easy touch targets.
-
Use
bindingAdapterPositioninside the listener rather than thepositioncaptured byonBindViewHolder, because recycled views are bound again for other positions.
Setting the adapter
Set the adapter and select a tab when it is clicked. Only the visible tab views are created, however many tabs the adapter reports:
// 10,000 tabs: only the visible tab views are created, and they are recycled on scroll
val titles = List(10_000) { "Tab $it" }
recyclerTabLayout.adapter = TabAdapter(titles) { position ->
recyclerTabLayout.setCurrentItem(position)
}
Selecting a tab
setCurrentItem(position, smoothScroll = true) selects a tab. The list scrolls to bring that tab into view and the
indicator moves under it. By default the indicator moves with an animation that lasts
RecyclerTabLayout.DEFAULT_SCROLL_DURATION_MILLIS (200 ms). Pass smoothScroll = false to jump straight there:
// scroll to tab 5 and animate the indicator towards it
recyclerTabLayout.setCurrentItem(5)
// select tab 5 immediately, without animation (for instance when restoring state)
recyclerTabLayout.setCurrentItem(5, smoothScroll = false)
Selecting tabs automatically while scrolling
By default, scrolling the tabs by hand does not change the selection. With automatic selection enabled, when a scroll gesture stops between two tabs, the tab closest to the centre of the view is selected:
// when a scroll gesture stops between two tabs, select the tab closest to the center
recyclerTabLayout.setAutoSelectionMode(true)
To let users scroll the tab strip at all, keep app:scrollEnabled at its default (true). See
XML attributes.
Keeping tabs in sync with a ViewPager2
RecyclerTabLayout doesn’t depend on any pager. Connect it to a ViewPager2 (or anything else that has pages)
with two callbacks: one from the pager to the tabs and one from the tabs to the pager:
// selecting a page selects the matching tab
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
recyclerTabLayout.setCurrentItem(position)
}
})
// clicking a tab shows the matching page
recyclerTabLayout.adapter = TabAdapter(titles) { position ->
viewPager.currentItem = position
}
When the pager selects a page, its callback calls setCurrentItem. When a tab is clicked, the adapter’s callback
changes the pager’s currentItem, and the pager callback then moves the indicator. You don’t need any extra
bookkeeping.
Customizing the indicator
Set the indicator’s color, height and margins in XML (see Configuration reference) or at
runtime. The values are in pixels, so convert from dp with the display density, then call invalidate() to redraw:
val density = recyclerTabLayout.resources.displayMetrics.density
// ARGB color, for instance ContextCompat.getColor(context, R.color.accent)
recyclerTabLayout.indicatorColor = Color.RED
recyclerTabLayout.indicatorHeight = (3 * density).toInt()
recyclerTabLayout.indicatorMarginLeft = (8 * density).toInt()
recyclerTabLayout.indicatorMarginRight = (8 * density).toInt()
recyclerTabLayout.invalidate()
To hide the indicator, set indicatorHeight to 0 or indicatorColor to Color.TRANSPARENT.