Skip to content

Instantly share code, notes, and snippets.

View Ahmad-Hamwi's full-sized avatar
🎯
Focusing

Ahmad Hamwi Ahmad-Hamwi

🎯
Focusing
View GitHub Profile
@Ahmad-Hamwi
Ahmad-Hamwi / applySystemBarsInsetOnPadding.kt
Created July 28, 2021 18:11
A useful binding adapter that will update the view's padding according to the system bar insets.
/**
* 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 July 28, 2021 18:14
A useful binding adapter that stretch the views boundaries to draw behind the system bar insets (aka going edge-to-edge)
/**
* 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}">
* ...
class Item(val content: String) {
}
class Category(val name: String, vararg item: Item) {
val listOfItems: List<Item> = item.toList()
}
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)
)
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 September 24, 2021 09:04
creating a list of categories, each category has a list of items
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")
),
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
initTabLayout()
initRecycler()
}
private fun initViews() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
initTabLayout()
initRecycler()
initMediator() // NEW
}
<?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"