Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Ahmad Hamwi Ahmad-Hamwi

🎯
Focusing
View GitHub Profile
@Ahmad-Hamwi
Ahmad-Hamwi / applySystemBarsInsetOnPadding.kt
Created Jul 28, 2021
A useful binding adapter that will update the view's padding according to the system bar insets.
View applySystemBarsInsetOnPadding.kt
/**
* Example:
*
* <!--parent container (that's expanded to fullscreen)-->
* <androidx.constraintlayout.widget.ConstraintLayout
* android:layout_width="match_parent"
* android:layout_height="match_parent"
* ...
* app:applySystemBarsInsetOnPadding="@{true}">
* ...
@Ahmad-Hamwi
Ahmad-Hamwi / setDecorFitSystemWindow.kt
Created Jul 28, 2021
A useful binding adapter that stretch the views boundaries to draw behind the system bar insets (aka going edge-to-edge)
View setDecorFitSystemWindow.kt
/**
* example:
*
* <!--parent container (that's expanded to fullscreen)-->
* <androidx.constraintlayout.widget.ConstraintLayout
* android:layout_width="match_parent"
* android:layout_height="match_parent"
* ...
* app:setDecorFitSystemWindow="@{true}">
* ...
View Item.kt
class Item(val content: String) {
}
View Category.kt
class Category(val name: String, vararg item: Item) {
val listOfItems: List<Item> = item.toList()
}
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 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)
)
}
@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 MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
initTabLayout()
initRecycler()
}
private fun initViews() {
View MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
initTabLayout()
initRecycler()
initMediator() // NEW
}
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"