Skip to content

Instantly share code, notes, and snippets.

@WilderMinds
WilderMinds / ListAdapterTemplate.kt
Last active July 10, 2021 01:06
Android Studio File Template Samples
#*
Thus begins a multi-line comment, the Velocity Templating Engine will
ignore it.
This template that can be used to quickly generate a ListAdapter and
DiffUtil to be used with your recyclerviews.
Setting imports are not essential because after the file is generated from
the template, the developer can always include the missing imports manually.
*#
package com.samdev.templates
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
class PersonListAdapter :
data class ViewNavigation (
val from: View,
val to: View
)
/**
* @param from the current visible page/viewGroup
* @param to the page/viewGroup you want to navigate to
*/
fun navigateViews(from: View, to: View) {
val viewNavigation = ViewNavigation(from, to)
navigationStack.push(viewNavigation)
navigate(viewNavigation)
}
@WilderMinds
WilderMinds / RandomViewsFragment.kt
Last active April 24, 2021 18:31
Navigating backwards when implementing the CustomNavigator
class RandomViewsFragment : Fragment() {
private val customNavigator = CustomNavigator()
/**
* Intercept onBackPressed
*/
private val onBackPressedCallback : OnBackPressedCallback = object : OnBackPressedCallback(true){
override fun handleOnBackPressed() {
back()
@WilderMinds
WilderMinds / CustomNavigator.kt
Created April 24, 2021 18:19
Custom navigator methods
/**
* Similar to the `onBackPressed()` method in activities
*
* Notice that the `from` and `to` params are switched when creating
* the viewNavigation object.
*
* We do this when we want to move in the opposite/reverse direction
*
* @return `true` if back navigation was successful and `false` if there's nothing else
* the user can trigger the default `onBackPressed()` method if false to hand control