Skip to content

Instantly share code, notes, and snippets.

View ElianFabian's full-sized avatar

Elián Fabián ElianFabian

View GitHub Profile
@DanielKnauf
DanielKnauf / medium_horizontal_recyclerview_in_viewpager.kt
Last active August 18, 2023 04:48
OnItemTouchListener blocking MotionEvent from RecyclerViews' parent if RecyclerView can scroll left or right
addOnItemTouchListener(
object : RecyclerView.OnItemTouchListener {
private var startX = 0f
override fun onInterceptTouchEvent(
recyclerView: RecyclerView,
event: MotionEvent
): Boolean =
when (event.action) {
MotionEvent.ACTION_DOWN -> { startX = event.x }
@fadhifatah
fadhifatah / AdaptiveSpacingItemDecoration.kt
Created March 8, 2022 07:59
New file: AdaptiveSpacingItemDecoration to help solve spacing problem inside RecyclerView
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
/**
* Give equal margin around each [RecyclerView] items, adaptively. Supports known direct subclasses of
@gmk57
gmk57 / 1 ViewBindingDelegates.kt
Last active May 29, 2024 10:42
Kotlin delegates for Android View Binding with usage examples
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
@aartikov
aartikov / DelegateAccess.kt
Created January 14, 2021 11:03
Wrap MutableStateFlow to property delegate
internal object DelegateAccess {
internal val delegate = ThreadLocal<Any?>()
internal val delegateRequested = ThreadLocal<Boolean>().apply { set(false) }
}
internal val <T> KProperty0<T>.delegate: Any?
get() {
try {
DelegateAccess.delegateRequested.set(true)
this.get()
class ForegroundServiceLauncher(private val serviceClass: Class<out Service>) {
private var isStarting = false
private var shouldStop = false
private var isCreated = false
@Synchronized
fun startService(context: Context, block: Intent.() -> Unit = {}) {
if (!isCreated) {
isStarting = true
@krishnabhargav
krishnabhargav / kotlin-sealedclass-serialization.kt
Last active May 15, 2024 12:52
Using GSON to support serialization and deserialization of Kotlin Sealed Classes.
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import kotlin.jvm.internal.Reflection
import kotlin.reflect.KClass
@objcode
objcode / ConcurrencyHelpers.kt
Last active July 7, 2024 09:57
Helpers to control concurrency for one shot requests using Kotlin coroutines.
/* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@afollestad
afollestad / BroadcastReceiver.kt
Last active January 14, 2024 10:23
A Lifecycle components aware BroadcastReceiver DSL (Kotlin)
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.lifecycle.Lifecycle.Event.ON_DESTROY
import androidx.lifecycle.Lifecycle.Event.ON_START
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import android.content.BroadcastReceiver as StockReceiver
@lawrence-laz
lawrence-laz / BaseBehaviour.cs
Created November 30, 2018 18:00
Automatically get components in Unity using [GetComponent] attribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
public class BaseBehaviour : MonoBehaviour
{
protected virtual void OnEnable()
{
@florina-muntenescu
florina-muntenescu / CustomTypefaceSpan.kt
Last active July 2, 2024 09:28
Style internationalized text using Annotations
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software