Kotlin委托機制使用方式和原理解析
類委托
類委托有點類似于Java中的代理模式
interface Base{ fun text() } //被委托的類(真實的類) class BaseImpl(val x:String): Base { override fun text() { println(x) } } //委托類 class Devices(b:Base):Base by b fun main(){ var b = BaseImpl("真實的類") Devices(b).text() }
輸出
委托類(代理類)持有真實類的對象,然后委托類(代理類)調(diào)用真實類的同名方法,最終真正實現(xiàn)的是方法的是真實類,這其實就是代理模式
kotlin中的委托借助于by關(guān)鍵字,by關(guān)鍵字后面就是被委托類
反編譯成java代碼
public final class BaseImpl implements Base { @NotNull private final String x; public BaseImpl(@NotNull String x) { Intrinsics.checkNotNullParameter(x, "x"); super(); this.x = x; } @NotNull public final String getX() { return this.x; } public void text() { String var1 = this.x; System.out.println(var1); } } // Devices.java package com.example.memoryoptimizing.delegate; import kotlin.Metadata; import kotlin.jvm.internal.Intrinsics; import org.jetbrains.annotations.NotNull; @Metadata( mv = {1, 9, 0}, k = 1, xi = 48, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0000\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0001¢\u0006\u0002\u0010\u0003J\t\u0010\u0004\u001a\u00020\u0005H\u0096\u0001¨\u0006\u0006"}, d2 = {"Lcom/example/memoryoptimizing/delegate/Devices;", "Lcom/example/memoryoptimizing/delegate/Base;", "b", "(Lcom/example/memoryoptimizing/delegate/Base;)V", "text", "", "app_debug"} ) public final class Devices implements Base { // $FF: synthetic field private final Base $$delegate_0; public Devices(@NotNull Base b) { Intrinsics.checkNotNullParameter(b, "b"); super(); this.$$delegate_0 = b; } public void text() { this.$$delegate_0.text(); } } // BaseImplKt.java package com.example.memoryoptimizing.delegate; import kotlin.Metadata; @Metadata( mv = {1, 9, 0}, k = 2, xi = 48, d1 = {"\u0000\b\n\u0000\n\u0002\u0010\u0002\n\u0000\u001a\u0006\u0010\u0000\u001a\u00020\u0001¨\u0006\u0002"}, d2 = {"main", "", "app_debug"} ) public final class BaseImplKt { public static final void main() { BaseImpl b = new BaseImpl("真實的類"); (new Devices((Base)b)).text(); } // $FF: synthetic method public static void main(String[] args) { main(); } }
可以看到,Devices持有BaseImpl對象,重寫text方法,text方法內(nèi)部調(diào)用的是BaseImpl.text()
屬性委托
屬性委托和類委托一樣,屬性委托其實是對屬性的set/get方法的委托,把set/get方法委托給setValue/getValue方法,因此被委托類(真實類)需要提供setValue/getValue方法,val屬性只需要提供setValue方法
屬性委托語法:
val/var <屬性名>:<類型> by <表達式>
class B{ //委托屬性 var a : String by Text() } class Text { operator fun getValue(thisRef: Any?, property: KProperty<*>): String { return "屬性擁有者 = $thisRef ,屬性的名字 = ‘${property.name}' 屬性的值" } operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { println("屬性的值 = $value 屬性的名字 = '${property.name}' 屬性擁有者 = $thisRef") } } fun main(){ var b = B() println(b.a) b.a = "ahaha" }
輸出
屬性a委托給了Text,而且Text類中有setValue和getValue,所有當我們調(diào)用屬性a的set/get方法時候,會委托到Text的setValue/getValue。
thisRef:屬性的擁有者
property:對屬性的描述,是KProperty<*>類型或者父類
value:屬性的值
反編譯成Java代碼
public final class B { // $FF: synthetic field static final KProperty[] $$delegatedProperties; @NotNull private final Text a$delegate = new Text(); @NotNull public final String getA() { return this.a$delegate.getValue(this, $$delegatedProperties[0]); } public final void setA(@NotNull String var1) { Intrinsics.checkNotNullParameter(var1, "<set-?>"); this.a$delegate.setValue(this, $$delegatedProperties[0], var1); } static { KProperty[] var0 = new KProperty[]{Reflection.mutableProperty1((MutablePropertyReference1)(new MutablePropertyReference1Impl(B.class, "a", "getA()Ljava/lang/String;", 0)))}; $$delegatedProperties = var0; } } // Text.java package com.example.memoryoptimizing.delegate; import kotlin.Metadata; import kotlin.jvm.internal.Intrinsics; import kotlin.reflect.KProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @Metadata( mv = {1, 9, 0}, k = 1, xi = 48, d1 = {"\u0000\"\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0002\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\u001f\u0010\u0003\u001a\u00020\u00042\b\u0010\u0005\u001a\u0004\u0018\u00010\u00012\n\u0010\u0006\u001a\u0006\u0012\u0002\b\u00030\u0007H\u0086\u0002J'\u0010\b\u001a\u00020\t2\b\u0010\u0005\u001a\u0004\u0018\u00010\u00012\n\u0010\u0006\u001a\u0006\u0012\u0002\b\u00030\u00072\u0006\u0010\n\u001a\u00020\u0004H\u0086\u0002¨\u0006\u000b"}, d2 = {"Lcom/example/memoryoptimizing/delegate/Text;", "", "()V", "getValue", "", "thisRef", "property", "Lkotlin/reflect/KProperty;", "setValue", "", "value", "app_debug"} ) public final class Text { @NotNull public final String getValue(@Nullable Object thisRef, @NotNull KProperty property) { Intrinsics.checkNotNullParameter(property, "property"); return "屬性擁有者 = " + thisRef + " ,屬性的名字 = ‘" + property.getName() + "' 屬性的值"; } public final void setValue(@Nullable Object thisRef, @NotNull KProperty property, @NotNull String value) { Intrinsics.checkNotNullParameter(property, "property"); Intrinsics.checkNotNullParameter(value, "value"); String var4 = "屬性的值 = " + value + " 屬性的名字 = '" + property.getName() + "' 屬性擁有者 = " + thisRef; System.out.println(var4); } } // TextKt.java package com.example.memoryoptimizing.delegate; import kotlin.Metadata; @Metadata( mv = {1, 9, 0}, k = 2, xi = 48, d1 = {"\u0000\b\n\u0000\n\u0002\u0010\u0002\n\u0000\u001a\u0006\u0010\u0000\u001a\u00020\u0001¨\u0006\u0002"}, d2 = {"main", "", "app_debug"} ) public final class TextKt { public static final void main() { B b = new B(); String var1 = b.getA(); System.out.println(var1); b.setA("ahaha"); } // $FF: synthetic method public static void main(String[] args) { main(); } }
可以看到B類持有Text對象,當調(diào)用B.get()方法,內(nèi)部調(diào)用了Text.getValue(),B中創(chuàng)建了KProperty來保存屬性的各種參數(shù)。
簡單的實現(xiàn)屬性委托
每次實現(xiàn)委托都要寫getValue/setValue方法,相對來說比較麻煩,Kotlin也提供了接口,方便我們重寫這些方法,ReadOnlyProperty和ReadWriterProperty
public fun interface ReadOnlyProperty<in T, out V> { /** * Returns the value of the property for the given object. * @param thisRef the object for which the value is requested. * @param property the metadata for the property. * @return the property value. */ public operator fun getValue(thisRef: T, property: KProperty<*>): V } /** * Base interface that can be used for implementing property delegates of read-write properties. * * This is provided only for convenience; you don't have to extend this interface * as long as your property delegate has methods with the same signatures. * * @param T the type of object which owns the delegated property. * @param V the type of the property value. */ public interface ReadWriteProperty<in T, V> : ReadOnlyProperty<T, V> { /** * Returns the value of the property for the given object. * @param thisRef the object for which the value is requested. * @param property the metadata for the property. * @return the property value. */ public override operator fun getValue(thisRef: T, property: KProperty<*>): V /** * Sets the value of the property for the given object. * @param thisRef the object for which the value is requested. * @param property the metadata for the property. * @param value the value to set. */ public operator fun setValue(thisRef: T, property: KProperty<*>, value: V) }
被委托類只需要實現(xiàn)接口重寫方法就行,val繼承ReadOnlyProperty
class Text1:ReadOnlyProperty<Any,String>{ override fun getValue(thisRef: Any, property: KProperty<*>): String { return "屬性擁有者 = $thisRef ,屬性的名字 = ‘${property.name}' 屬性的值" } } class Text2: ReadWriteProperty<Any,String>{ override fun getValue(thisRef: Any, property: KProperty<*>): String { return "屬性擁有者 = $thisRef ,屬性的名字 = ‘${property.name}' 屬性的值" } override fun setValue(thisRef: Any, property: KProperty<*>, value: String) { println("屬性的值 = $value 屬性的名字 = '${property.name}' 屬性擁有者 = $thisRef") } }
class B{ val b :String by Text1() var c : String by Text2() }
fun main(){ var b = B() b.c = "1" }
Kotlin標準庫中提供的幾個委托
- 延遲屬性(lazy properties):其值只在訪問時計算
- 可觀察屬性(observable properties):監(jiān)聽器會收到此屬性的變更通知
- 把多個屬性映射到Map中,而不存在單個字段
延遲屬性Lazy
lazy()接收一個lambda,返回Lazy實例,返回的實例可以作為實現(xiàn)延遲屬性的委托,僅在第一次調(diào)用屬性進行初始化
class Lazy{ val name:String by lazy(LazyThreadSafetyMode.SYNCHRONIZED){ println("第一次初始化") "aa" } } fun main(){ var lazy = Lazy() println(lazy.name) println(lazy.name) }
反編譯Java代碼
public final class Lazy { @NotNull private final kotlin.Lazy name$delegate; public Lazy() { this.name$delegate = kotlin.LazyKt.lazy( (Function0)null.INSTANCE); } @NotNull public final String getName() { kotlin.Lazy var1 = this.name$delegate; Object var2 = null; return (String)var1.getValue(); } } // LazyKt.java package com.example.memoryoptimizing.delegate; import kotlin.Metadata; @Metadata( mv = {1, 9, 0}, k = 2, xi = 48, d1 = {"\u0000\b\n\u0000\n\u0002\u0010\u0002\n\u0000\u001a\u0006\u0010\u0000\u001a\u00020\u0001¨\u0006\u0002"}, d2 = {"main", "", "app_debug"} ) public final class LazyKt { public static final void main() { Lazy lazy = new Lazy(); String var1 = lazy.getName(); System.out.println(var1); var1 = lazy.getName(); System.out.println(var1); } // $FF: synthetic method public static void main(String[] args) { main(); } }
發(fā)現(xiàn)Lazy再初始化時生成了name $ delegate,變量是Kotlin.Lazy類型的,而getName()方法返回的其實就是name $ delegate.getValue()
name $ delegate是由kotlin.LazyKt.lazy (Function0)null.INSTANCE);生成的,可以看一下源碼
public actual fun <T> lazy(initializer: () -> T): Lazy<T> = SynchronizedLazyImpl(initializer)
最終是由SynchronizedLazyImpl生成
private class SynchronizedLazyImpl<out T>(initializer: () -> T, lock: Any? = null) : Lazy<T>, Serializable { private var initializer: (() -> T)? = initializer @Volatile private var _value: Any? = UNINITIALIZED_VALUE // final field is required to enable safe publication of constructed instance private val lock = lock ?: this override val value: T get() { val _v1 = _value if (_v1 !== UNINITIALIZED_VALUE) { @Suppress("UNCHECKED_CAST") return _v1 as T } return synchronized(lock) { val _v2 = _value if (_v2 !== UNINITIALIZED_VALUE) { @Suppress("UNCHECKED_CAST") (_v2 as T) } else { val typedValue = initializer!!() _value = typedValue initializer = null typedValue } } } override fun isInitialized(): Boolean = _value !== UNINITIALIZED_VALUE override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet." private fun writeReplace(): Any = InitializedLazyImpl(value) }
可以直接看value的get方法,如果_v1 !== UNINITIALIZED_VALUE則表明已經(jīng)初始化過了,就直接返回value,否則表明沒有初始化過,調(diào)用initializer方法,也就是lazy的lambda表達式
Lazy委托參數(shù)
public enum class LazyThreadSafetyMode { /** * Locks are used to ensure that only a single thread can initialize the [Lazy] instance. */ SYNCHRONIZED, /** * Initializer function can be called several times on concurrent access to uninitialized [Lazy] instance value, * but only the first returned value will be used as the value of [Lazy] instance. */ PUBLICATION, /** * No locks are used to synchronize an access to the [Lazy] instance value; if the instance is accessed from multiple threads, its behavior is undefined. * * This mode should not be used unless the [Lazy] instance is guaranteed never to be initialized from more than one thread. */ NONE, }
- SYNCHRONIZED:添加同步鎖,使lazy延遲初始化線程安全
- PUBLICATION:初始化的lambda表達式,可以在同一時間多次調(diào)用,但是只有第一次的返回值作為初始化值
- NONE:沒有同步鎖,非線程安全
使用
val name :String by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { println("第一次調(diào)用初始化") "aa" } }
可觀察屬性O(shè)bservable委托
可以觀察一個屬性的變化過程
class Observable { var a:String by Delegates.observable("默認值"){ property, oldValue, newValue -> println("${oldValue} -> ${newValue}") } } fun main(){ var observable = Observable() observable.a = "第一次修改的值" observable.a = "第二次修改的值" }
vetoable委托
vetoable和Observable一樣,可以觀察屬性的變化,不同的是vetoable可以決定是否使用新值
class Vetoable { var age:Int by Delegates.vetoable(0){ property, oldValue, newValue -> println("oldValue = $oldValue -> oldValue = $newValue" ) newValue > oldValue } } fun main() { var c = Vetoable() c.age = 5 println(c.age) c.age = 10 println(c.age) c.age = 8 println(c.age) c.age = 20 println(c.age) }
可以看到,當新值小于舊值,就會不生效
屬性儲存在Map中
class D(val map:Map<String,Any?>){ val name:String by map val age:Int by map } fun main() { var d = D( mapOf( "name" to "小明", "age" to 12 ) ) println("name = ${d.name},age = ${d.age}") }
實踐方式
雙擊back退出
private var backPressedTime by Delegates.observable(0L){pre,old,new -> //2次的時間間隔小于2秒就退出了 if(new - old < 2000){ finish() }else{ Toast.makeText(this,"再按返回鍵退出",Toast.LENGTH_LONG) } } override fun onBackPressed() { super.onBackPressed() backPressedTime = System.currentTimeMillis() }
Fragment/Activity傳參
在項目中經(jīng)常需要給Fragment/Activity傳遞參數(shù)
模版代碼
private const val ARG_PARAM1 = "param1" private const val ARG_PARAM2 = "param2" class DelegateFragment : Fragment() { // TODO: Rename and change types of parameters private var param1: String? = null private var param2: String? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { param1 = it.getString(ARG_PARAM1) param2 = it.getString(ARG_PARAM2) } } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_delegate, container, false) } companion object { /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment DelegateFragment. */ // TODO: Rename and change types and number of parameters @JvmStatic fun newInstance(param1: String, param2: String) = DelegateFragment().apply { arguments = Bundle().apply { putString(ARG_PARAM1, param1) putString(ARG_PARAM2, param2) } } } }
我們可以把參數(shù)賦值和獲取的代碼抽取委托類,然后把param1和param2聲明為委托屬性
修改后的Fragment代碼
class DelegateFragment : Fragment() { private var param1: String? by argumentNullable() private var param2: String by argument("1") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d("DelegateFragment","param1 ${param1} param2 ${param2}") } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_delegate, container, false) } companion object { /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment DelegateFragment. */ // TODO: Rename and change types and number of parameters @JvmStatic fun newInstance(param1: String, param2: String) = DelegateFragment().apply { this.param1 = param1 this.param2 = param2 } } }
委托類
import android.os.Bundle import android.os.Parcelable import android.util.Log import androidx.fragment.app.Fragment import java.io.Serializable import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty fun <T> Fragment.argumentNullable() = FragmentArgumentPropertyNullable<T>() fun <T> Fragment.argument(defaultValue:T? = null) = FragmentArgumentProperty<T>(defaultValue) class FragmentArgumentPropertyNullable<T> : ReadWriteProperty<Fragment,T?>{ override fun getValue(thisRef: Fragment, property: KProperty<*>): T? { return thisRef.arguments?.getValue(property.name) } override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T?) { Log.d("DelegateFragment","property.name ${property.name} thisRef.arguments ${thisRef.arguments}") val arguments = thisRef.arguments?:Bundle().also { thisRef.arguments = it } if(arguments.containsKey(property.name)){ return } arguments[property.name] = value } } class FragmentArgumentProperty<T> (private val defaultValue: T? = null): ReadWriteProperty<Fragment,T>{ override fun getValue(thisRef: Fragment, property: KProperty<*>): T { return thisRef.arguments?.getValue(property.name) as? T ?:defaultValue ?:throw IllegalStateException("Property ${property.name} could not be read") } override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) { val arguments = thisRef.arguments?:Bundle().also { thisRef.arguments = it } if(arguments.containsKey(property.name)){ return } arguments[property.name] = value } } fun <T> Bundle.getValue(key:String):T?{ return get(key) as T? } //操作符重載a[i] = b set() a.set(i, b) operator fun <T> Bundle.set(key: String, value: T?) { when (value) { is Boolean -> putBoolean(key, value) is Byte -> putByte(key, value) is Char -> putChar(key, value) is Short -> putShort(key, value) is Int -> putInt(key, value) is Long -> putLong(key, value) is Float -> putFloat(key, value) is Double -> putDouble(key, value) is String? -> putString(key, value) is CharSequence? -> putCharSequence(key, value) is Serializable? -> putSerializable(key, value) // also ArrayList is Parcelable? -> putParcelable(key, value) is Bundle? -> putBundle(key, value) is BooleanArray? -> putBooleanArray(key, value) is ByteArray? -> putByteArray(key, value) is CharArray? -> putCharArray(key, value) is ShortArray? -> putShortArray(key, value) is IntArray? -> putIntArray(key, value) is LongArray? -> putLongArray(key, value) is FloatArray? -> putFloatArray(key, value) is DoubleArray? -> putDoubleArray(key, value) is ArrayList<*>? -> throw IllegalStateException("ArrayList<*> $key is not supported") is Array<*>? -> throw IllegalStateException("Array<*> $key is not supported") else -> throw IllegalStateException("Type $key is not supported") } }
相比于常規(guī)的寫法,使用屬性委托優(yōu)勢會相對明顯,不需要定義Key字符串,而是使用變量名作為Key。不再需要編寫向Argument設(shè)置參數(shù)和讀取參數(shù)的代碼,聲明可空參數(shù)時也可以聲明默認值。
ViewBinding和委托
在Fragment中使用
class DelegateFragment : Fragment(R.layout.fragment_delegate) { private var _binding: FragmentDelegateBinding? = null private val binding get() = _binding!! override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) _binding = FragmentDelegateBinding.bind(view) binding.tvName.text = "This is Hello World" } override fun onDestroyView() { super.onDestroyView() //置空,防止內(nèi)存泄漏 _binding = null } }
ViewBinding綁定類的源碼,反編譯如下:
public final class FragmentDelegateBinding implements ViewBinding { private final ConstraintLayout rootView; public final TextView tvDisplay; private FragmentDelegateBinding(ConstraintLayout paramConstraintLayout1, TextView paramTextView) this.rootView = paramConstraintLayout1; this.tvDisplay = paramTextView; } public static FragmentDelegateBindingbind(View paramView) { TextView localTextView = (TextView)paramView.findViewById(2131165363); if (localTextView != null) { return new ActivityMainBinding((ConstraintLayout)paramView, localTextView); }else { paramView = "tvDisplay"; } throw new NullPointerException("Missing required view with ID: ".concat(paramView)); } public static FragmentDelegateBinding inflate(LayoutInflater paramLayoutInflater) { return inflate(paramLayoutInflater, null, false); } public static FragmentDelegateBinding inflate(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, boolean paramBoolean) { paramLayoutInflater = paramLayoutInflater.inflate(2131361821, paramViewGroup, false); if (paramBoolean) { paramViewGroup.addView(paramLayoutInflater); } return bind(paramLayoutInflater); } public ConstraintLayout getRoot() { return this.rootView; } }
通過委托的方式進行優(yōu)化
- 委托ViewBinding.bind()的調(diào)用 -> 反射
- 委托destroy時binding = null的調(diào)用 -> 監(jiān)聽Fragment視圖生命周期
- 想要binding屬性聲明為非空不可變變量val -> 屬性委托ReadOnlyProperty<F,V>
編寫委托類,詳細內(nèi)容可看注釋
package com.example.memoryoptimizing.delegate import android.os.Handler import android.os.Looper import android.util.Log import android.view.View import androidx.fragment.app.Fragment import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.LifecycleOwner import androidx.viewbinding.ViewBinding import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty //為什么使用inline fun<reified V>,方便直接拿到V::class.java /** * fun <V> printClass() { * println(V::class.java) // ? 編譯錯誤:Cannot access 'java.lang.Class' for a type parameter V * } * inline fun <reified V> printClass() { * println(V::class.java) // ? 輸出如:class kotlin.String * } */ //在屬性委托中,編譯器可以通過屬性聲明的類型,如 FragmentDelegateBinding)來推斷泛型函數(shù)中的具體類型參數(shù); // 而在普通函數(shù)調(diào)用中,僅憑返回值或賦值目標無法反推出泛型參數(shù)的具體類型。 /** ? 場景一:屬性委托 + reified 泛型函數(shù) ? 可以推斷 private val binding: FragmentDelegateBinding by viewBindingV1() ? 場景二:普通函數(shù)調(diào)用 ? 無法推斷 inline fun <reified V> getTypeName(): String { return V::class.java.name } val name: String = getTypeName() */ private const val TAG = "ViewBindingProperty" //使用inline fun <reified V>可以在調(diào)用泛型函數(shù)時省略參數(shù)的傳遞,Kotlin會自動根據(jù)泛型類型幫你找到對應(yīng)的class<T> public inline fun <reified V:ViewBinding> viewBindingV1() = viewBindingV1(V::class.java) public inline fun <reified T:ViewBinding> viewBindingV1(clazz:Class<T>):FragmentViewBindingProperttV1<Fragment,T>{ val bindMethod = clazz.getMethod("bind", View::class.java) return FragmentViewBindingProperttV1{ fragment-> /** * 調(diào)用靜態(tài)方法bind(View view),第一個參數(shù)為null(因為是靜態(tài)方法) 第二個參數(shù)是View,來自Fragment的requireView() * as T 將結(jié)果強制轉(zhuǎn)換為泛型T,即具體的ViewBinding子類(如FragmentDelegateBinding) * FragmentDelegateBinding.bind(Fragment.requireView()) */ bindMethod.invoke(null,fragment.requireView()) as T } } /** * viewBinder 創(chuàng)建綁定類對象 */ class FragmentViewBindingProperttV1<in F:Fragment,out V: ViewBinding>( private val viewBinder:(F) ->V //給定一個 Fragment(或其子類),返回一個對應(yīng)的 ViewBinding 實例 ):ReadOnlyProperty<F,V>{ private var viewBinding:V? = null override fun getValue(thisRef: F, property: KProperty<*>): V { //viewBinding不為空說明已經(jīng)綁定,直接返回 viewBinding?.let { return it } //Fragment視圖的生命周期 val lifecycle = thisRef.viewLifecycleOwner.lifecycle //實例化綁定類對象 val viewBinding = viewBinder(thisRef) if(lifecycle.currentState == Lifecycle.State.DESTROYED){ Log.w( TAG, "Access to viewBinding after Lifecycle is destroyed or hasn't created yet. " + "The instance of viewBinding will be not cached." ) }else{ lifecycle.addObserver(ClearOnDestroyLifecycleObserver()) this.viewBinding = viewBinding } return viewBinding } fun clear(){ viewBinding = null } private inner class ClearOnDestroyLifecycleObserver : LifecycleObserver{ private val mainHandler = Handler(Looper.getMainLooper()) fun onDestroy(owner:LifecycleOwner){ owner.lifecycle.removeObserver(this) mainHandler.post { clear() } } } }
使用例子:
class DelegateFragment : Fragment(R.layout.fragment_delegate) { private val binding : FragmentDelegateBinding by viewBindingV1() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.tvName.text = "This is Hello World" } }
不使用反射的方式,反射調(diào)用bind函數(shù)的主要目的是獲得一個ViewBinding綁定類對象,我們可以把創(chuàng)建對象的行為交給外部去定義
inline fun <F:Fragment,V:ViewBinding> viewBindingV2( crossinline viewBinder:(View) -> V,//接受一個View,返回Binding實例 crossinline viewProvider:(F) -> View = { fragment -> fragment.requireView() //這里的fragment就是F } //接受一個Fragment,返回它的View ) = FragmentViewBindingPropertyV2{ fragment:F -> viewBinder(viewProvider(fragment)) //FragmentDelegateBinding.bind(Fragment.requireView()) }//fragment它是 Kotlin 屬性委托機制在訪問 binding 屬性時自動傳入的當前 Fragment 實例 class FragmentViewBindingPropertyV2<in F:Fragment , out V: ViewBinding>( private val viewBinder:(F) -> V ):ReadOnlyProperty<F,V>{ private var viewBinding: V? = null override fun getValue(thisRef: F, property: KProperty<*>): V { //viewBinding不為空說明已經(jīng)綁定,直接返回 viewBinding?.let { return it } //Fragment視圖的生命周期 val lifecycle = thisRef.viewLifecycleOwner.lifecycle //實例化綁定類對象 val viewBinding = viewBinder(thisRef) if (lifecycle.currentState == Lifecycle.State.DESTROYED) { Log.w( TAG, "Access to viewBinding after Lifecycle is destroyed or hasn't created yet. " + "The instance of viewBinding will be not cached." ) } else { lifecycle.addObserver(ClearOnDestroyLifecycleObserver()) this.viewBinding = viewBinding } return viewBinding } fun clear() { viewBinding = null } private inner class ClearOnDestroyLifecycleObserver : LifecycleObserver { private val mainHandler = Handler(Looper.getMainLooper()) fun onDestroy(owner: LifecycleOwner) { owner.lifecycle.removeObserver(this) mainHandler.post { clear() } } } }
使用方式
class DelegateFragment : Fragment(R.layout.fragment_delegate) { private val binding : FragmentDelegateBinding by viewBindingV2(FragmentDelegateBinding::bind) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.tvName.text = "This is Hello World" } }
到此這篇關(guān)于Kotlin委托機制使用方式和原理的文章就介紹到這了,更多相關(guān)Kotlin委托內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JSON字符串轉(zhuǎn)成java的Map對象詳細步驟
這篇文章主要介紹了如何將JSON字符串轉(zhuǎn)換為Java對象的步驟,包括定義Element類、使用Jackson庫解析JSON和添加依賴,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-01-01IDEA啟動tomcat控制臺中文亂碼問題的解決方法(100%有效)
很多人在idea中啟動項目時會出現(xiàn)控制臺的中文亂碼,其實也無傷大雅,但是本人看著不舒服,下面這篇文章主要給大家介紹了關(guān)于IDEA啟動tomcat控制臺中文亂碼問題的解決方法,需要的朋友可以參考下2022-09-09logback標記日志過濾器MarkerFilter源碼解讀
這篇文章主要為大家介紹了logback標記日志過濾器MarkerFilter源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11SpringBoot整合Druid數(shù)據(jù)源過程詳解
這篇文章主要介紹了SpringBoot整合Druid數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12Java中回調(diào)函數(shù)?(callback)?及其實際應(yīng)用場景
在Java中回調(diào)函數(shù)(Callback)是一種常見的設(shè)計模式,用于實現(xiàn)異步操作或事件處理,這篇文章主要給大家介紹了關(guān)于Java中回調(diào)函數(shù)?(callback)?及其實際應(yīng)用場景的相關(guān)資料,需要的朋友可以參考下2024-02-02解讀為什么@Autowired在屬性上被警告,在setter方法上不被警告問題
在Spring開發(fā)中,@Autowired注解常用于實現(xiàn)依賴注入,它可以應(yīng)用于類的屬性、構(gòu)造器或setter方法上,然而,當@Autowired注解在屬性上使用時,IntelliJIDEA等IDE會給出Fieldinjectionisnotrecommended的警告,而在setter方法上使用@Autowired時卻不會出現(xiàn)這個警告2025-02-02