Created
August 7, 2020 03:50
-
-
Save Krosxx/0fd4bcaa84922abb78569f438e573f17 to your computer and use it in GitHub Desktop.
View 属性代理
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.view.View | |
import android.widget.Spinner | |
import android.widget.TextView | |
import kotlin.reflect.KProperty | |
/** | |
* View 属性代理 | |
* | |
* @param V : View | |
* @param T | |
* @property v V | |
* @property getter | |
* @property setter | |
* @constructor | |
*/ | |
open class ViewPropertiesDelegate<V : View, T>( | |
val v: V, | |
val getter: (v: V) -> T, | |
val setter: (t: T, v: V) -> Unit | |
) { | |
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = getter(v) | |
operator fun setValue(thisRef: Any?, property: KProperty<*>, t: T) = setter(t, v) | |
} | |
class TextViewDel(v: TextView) : ViewPropertiesDelegate<TextView, String>(v, | |
setter = { t, v -> v.text = t }, | |
getter = { it.text.toString() } | |
) | |
class SpinnerDel(v: Spinner) : ViewPropertiesDelegate<Spinner, Int>( | |
v, setter = { t, v -> v.setSelection(t) }, | |
getter = { v -> v.selectedItemPosition } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: