A mediator class used to link tabs in TabLayout to items in a RecyclerView. This can be used to map different sections of a RecyclerView to a common header represented by a Tab in TabLayout.
- Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
- Add the dependency
dependencies {
implementation 'com.github.ShivanshPrakash:TabLayoutMediator-RecyclerView:{version}'
}
- Create an instance of TabLayoutRecyclerViewMediator
val mediator = TabLayoutRecyclerViewMediator(recyclerView,
headerCount,
tabLayout,
headerToItemMapping,
itemToHeaderMapping) { tab, position ->
tab.text = categoryList[position]
}
- Call
attach()
function.
mediator.attach()
The constructor parameters for TabLayoutRecyclerViewMediator are described below -
Parameter | Description | Type |
---|---|---|
recyclerView |
RecyclerView used in mediator. | RecyclerView |
headerCount |
Count of headers (tabs). | Int |
tabLayout |
TabLayout used in mediator. | TabLayout |
headerToItemMapping |
Used to map index of tab in tabLayout to index of item in recyclerView . |
((headerPosition: Int) -> Int? |
itemToHeaderMapping |
Used to map index of item in recyclerView to index of tab in tabLayout . |
(itemPosition: Int) -> Int? |
autoRefresh |
If true tabLayout is reset whenever there is a change in adapter attached to recyclerView . Default value is true. |
Boolean |
tabConfigurationStrategy |
Used to configure the tab at the specified position. | (tab: TabLayout.Tab, position: Int) -> Unit |