Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Ahmad Hamwi Ahmad-Hamwi

🎯
Focusing
View GitHub Profile
View MainActivity.kt
private fun initMediator() {
TabbedListMediator(
recyclerView,
tabLayout,
categories.indices.toList(),
true // NEW
).attach()
}
View build.gradle
dependencies {
implementation 'io.github.ahmad-hamwi:tabsync:1.0.1'
}
View build.gradle
allprojects {
repositories {
...
mavenCentral()
}
}
View activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
View MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
initTabLayout()
initRecycler()
initMediator() // NEW
}
View MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
initTabLayout()
initRecycler()
}
private fun initViews() {
@Ahmad-Hamwi
Ahmad-Hamwi / MainActivity.kt
Created Sep 24, 2021
creating a list of categories, each category has a list of items
View MainActivity.kt
private val categories = mutableListOf(
Category(
"Category 1",
Item("Item 1"),
Item("Item 2"),
Item("Item 3"),
Item("Item 4"),
Item("Item 5"),
Item("Item 6")
),
View CategoriesAdapter.kt
class CategoriesAdapter(
private val context: Context,
private val listOfCategories: List<Category>
) : RecyclerView.Adapter<CategoriesAdapter.CategoryViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
return CategoryViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_category, parent, false)
)
}
View ItemsAdapter.kt
class ItemsAdapter(
private val context: Context,
private var items: List<Item>
) :
RecyclerView.Adapter<ItemsAdapter.ItemViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
return ItemViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_item, parent, false)
)
View Category.kt
class Category(val name: String, vararg item: Item) {
val listOfItems: List<Item> = item.toList()
}