Created
May 6, 2020 08:28
-
-
Save lgawin/2636ce0442d4bfb976df16e146e57757 to your computer and use it in GitHub Desktop.
Delegate for finding view by id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.view.View | |
import androidx.annotation.IdRes | |
import androidx.fragment.app.Fragment | |
fun <T : View> Activity.findView(@IdRes res : Int) : Lazy<T> { | |
@Suppress("UNCHECKED_CAST") | |
return lazy(LazyThreadSafetyMode.NONE){ findViewById<T>(res) } | |
} | |
fun <T : View> View.findView(@IdRes res : Int) : Lazy<T> { | |
@Suppress("UNCHECKED_CAST") | |
return lazy(LazyThreadSafetyMode.NONE){ findViewById<T>(res) } | |
} | |
fun <T : View> Fragment.findView(@IdRes res : Int) : Lazy<T> { | |
@Suppress("UNCHECKED_CAST") | |
return lazy(LazyThreadSafetyMode.NONE){ requireView().findViewById<T>(res) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: e.g. declare (delegated) property in Fragment
It can also be used for views. E.g. in ViewHolder declare: