Skip to content

Instantly share code, notes, and snippets.

@sockeqwe
sockeqwe / AdapterDelegateDslExample.kt
Created July 22, 2019 08:19
AdapterDelegates Kotlin DSL
/**
* Let's say we want to display a list of Animals in a RecyclerView.
* The list can contain items of type Dog, Cat and Snake.
*/
// AdapterDelegate for Dog.
fun dogAdapterDelegate(picasso : Picasso) = adapterDelegate<Dog, Animal>(R.layout.item_dog){ // Generics Types means this AdapterDelegate is used if item is instanceof Dog (whole list is List<Anmial>)
// this block is run once in onCreateViewHolder. Think of it as an intializer for a ViewHolder.
val name = findViewById(R.id.name)
val image = findViewById(R.id.image)
@nikhil-thakkar
nikhil-thakkar / SampleActivity.kt
Last active March 28, 2023 08:25
Generic databinding adapter for items in Recyclerview
import androidx.recyclerview.widget.RecyclerView
class SampleActivity: Activity(), OnItemActionListener {
private val adapter by lazy { ViewTypeAdapter<ViewType<*>>(list = list, onItemActionListener = this) }
//Prepare the list somehow, whether in ViewModel or presenter and then set it to adapter using setList
private val list: List<ViewType<*>> by lazy {
arrayListOf<>(HeaderViewType("Header"),
RowViewType("content"),
@wispborne
wispborne / Preferences.kt
Last active July 21, 2022 03:47
Android SharedPreferences helper class for Kotlin. Easy-to-use delegated properties, automatic database creation, and listening for property changes.
import android.content.Context
import android.content.SharedPreferences
import kotlin.reflect.KProperty
/**
* Represents a single [SharedPreferences] file.
*
* Usage:
*
* ```kotlin
@gunhansancar
gunhansancar / LocaleHelper.java
Last active April 8, 2021 12:34
While developing your awesome application, sometimes you are required to add a language change feature to your app on the fly. However, Android OS does not directly support this behaviour. And therefore, you need to solve this situation in some other ways. For more details see http://gunhansancar.com/change-language-programmatically-in-android/
package com.gunhansancar.changelanguageexample.helper;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;