Skip to content

Instantly share code, notes, and snippets.

@scruffyfox
Created July 17, 2017 09:29
Show Gist options
  • Save scruffyfox/5308c84912ab8e7baa3cc0fbe0d3c35d to your computer and use it in GitHub Desktop.
Save scruffyfox/5308c84912ab8e7baa3cc0fbe0d3c35d to your computer and use it in GitHub Desktop.
Kotlin utils
package com.cube.lib.util
import android.app.Activity
import android.support.annotation.IdRes
import android.support.annotation.LayoutRes
import android.support.v4.app.Fragment
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* Extension functions for View class
*/
fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T>
{
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById(idRes) as T }
}
fun <T : View> Fragment.bind(@IdRes idRes: Int): Lazy<T>
{
@Suppress("UNCHECKED_CAST")
return unsafeLazy { view?.findViewById(idRes) as T }
}
fun <T : View> View.bind(@IdRes idRes: Int): Lazy<T>
{
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById(idRes) as T }
}
fun <T : View> RecyclerView.ViewHolder.bind(@IdRes idRes: Int): Lazy<T>
{
@Suppress("UNCHECKED_CAST")
return unsafeLazy { itemView.findViewById(idRes) as T }
}
fun <T : View> bind(@IdRes idRes: Int, parent : View): Lazy<T>
{
@Suppress("UNCHECKED_CAST")
return unsafeLazy { parent.findViewById(idRes) as T }
}
/**
* Convenience method for inflating a view into another. Will return the inflated view, or the parent view if attach = true
*/
fun <T : View> View.inflate(@LayoutRes layoutRes: Int, attach: Boolean = false) : T
{
@Suppress("UNCHECKED_CAST")
return LayoutInflater.from(context).inflate(layoutRes, this as ViewGroup, attach) as T
}
private fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment