Kotlin委托機(jī)制使用方式和原理解析
類(lèi)委托
類(lèi)委托有點(diǎn)類(lèi)似于Java中的代理模式
interface Base{
fun text()
}
//被委托的類(lèi)(真實(shí)的類(lèi))
class BaseImpl(val x:String): Base {
override fun text() {
println(x)
}
}
//委托類(lèi)
class Devices(b:Base):Base by b
fun main(){
var b = BaseImpl("真實(shí)的類(lèi)")
Devices(b).text()
}輸出

委托類(lèi)(代理類(lèi))持有真實(shí)類(lèi)的對(duì)象,然后委托類(lèi)(代理類(lèi))調(diào)用真實(shí)類(lèi)的同名方法,最終真正實(shí)現(xiàn)的是方法的是真實(shí)類(lèi),這其實(shí)就是代理模式
kotlin中的委托借助于by關(guān)鍵字,by關(guān)鍵字后面就是被委托類(lèi)
反編譯成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("真實(shí)的類(lèi)");
(new Devices((Base)b)).text();
}
// $FF: synthetic method
public static void main(String[] args) {
main();
}
}可以看到,Devices持有BaseImpl對(duì)象,重寫(xiě)text方法,text方法內(nèi)部調(diào)用的是BaseImpl.text()
屬性委托
屬性委托和類(lèi)委托一樣,屬性委托其實(shí)是對(duì)屬性的set/get方法的委托,把set/get方法委托給setValue/getValue方法,因此被委托類(lèi)(真實(shí)類(lèi))需要提供setValue/getValue方法,val屬性只需要提供setValue方法
屬性委托語(yǔ)法:
val/var <屬性名>:<類(lèi)型> by <表達(dá)式>
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類(lèi)中有setValue和getValue,所有當(dāng)我們調(diào)用屬性a的set/get方法時(shí)候,會(huì)委托到Text的setValue/getValue。
thisRef:屬性的擁有者
property:對(duì)屬性的描述,是KProperty<*>類(lèi)型或者父類(lèi)
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類(lèi)持有Text對(duì)象,當(dāng)調(diào)用B.get()方法,內(nèi)部調(diào)用了Text.getValue(),B中創(chuàng)建了KProperty來(lái)保存屬性的各種參數(shù)。
簡(jiǎn)單的實(shí)現(xiàn)屬性委托
每次實(shí)現(xiàn)委托都要寫(xiě)getValue/setValue方法,相對(duì)來(lái)說(shuō)比較麻煩,Kotlin也提供了接口,方便我們重寫(xiě)這些方法,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)
}被委托類(lèi)只需要實(shí)現(xiàn)接口重寫(xiě)方法就行,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標(biāo)準(zhǔn)庫(kù)中提供的幾個(gè)委托
- 延遲屬性(lazy properties):其值只在訪問(wèn)時(shí)計(jì)算
- 可觀察屬性(observable properties):監(jiān)聽(tīng)器會(huì)收到此屬性的變更通知
- 把多個(gè)屬性映射到Map中,而不存在單個(gè)字段
延遲屬性Lazy
lazy()接收一個(gè)lambda,返回Lazy實(shí)例,返回的實(shí)例可以作為實(shí)現(xiàn)延遲屬性的委托,僅在第一次調(diào)用屬性進(jìn)行初始化
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再初始化時(shí)生成了name $ delegate,變量是Kotlin.Lazy類(lèi)型的,而getName()方法返回的其實(shí)就是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)初始化過(guò)了,就直接返回value,否則表明沒(méi)有初始化過(guò),調(diào)用initializer方法,也就是lazy的lambda表達(dá)式
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表達(dá)式,可以在同一時(shí)間多次調(diào)用,但是只有第一次的返回值作為初始化值
- NONE:沒(méi)有同步鎖,非線程安全
使用
val name :String by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
println("第一次調(diào)用初始化")
"aa" }
}可觀察屬性O(shè)bservable委托
可以觀察一個(gè)屬性的變化過(guò)程
class Observable {
var a:String by Delegates.observable("默認(rèn)值"){
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)
}
可以看到,當(dāng)新值小于舊值,就會(huì)不生效
屬性儲(chǔ)存在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}")
}
實(shí)踐方式
雙擊back退出
private var backPressedTime by Delegates.observable(0L){pre,old,new ->
//2次的時(shí)間間隔小于2秒就退出了
if(new - old < 2000){
finish()
}else{
Toast.makeText(this,"再按返回鍵退出",Toast.LENGTH_LONG)
}
}
override fun onBackPressed() {
super.onBackPressed()
backPressedTime = System.currentTimeMillis()
}Fragment/Activity傳參
在項(xiàng)目中經(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ù)賦值和獲取的代碼抽取委托類(lèi),然后把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
}
}
}委托類(lèi)
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ī)的寫(xiě)法,使用屬性委托優(yōu)勢(shì)會(huì)相對(duì)明顯,不需要定義Key字符串,而是使用變量名作為Key。不再需要編寫(xiě)向Argument設(shè)置參數(shù)和讀取參數(shù)的代碼,聲明可空參數(shù)時(shí)也可以聲明默認(rèn)值。
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綁定類(lèi)的源碼,反編譯如下:
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;
}
}通過(guò)委托的方式進(jìn)行優(yōu)化
- 委托ViewBinding.bind()的調(diào)用 -> 反射
- 委托destroy時(shí)binding = null的調(diào)用 -> 監(jiān)聽(tīng)Fragment視圖生命周期
- 想要binding屬性聲明為非空不可變變量val -> 屬性委托ReadOnlyProperty<F,V>
編寫(xiě)委托類(lèi),詳細(xì)內(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) // ? 編譯錯(cuò)誤:Cannot access 'java.lang.Class' for a type parameter V
* }
* inline fun <reified V> printClass() {
* println(V::class.java) // ? 輸出如:class kotlin.String
* }
*/
//在屬性委托中,編譯器可以通過(guò)屬性聲明的類(lèi)型,如 FragmentDelegateBinding)來(lái)推斷泛型函數(shù)中的具體類(lèi)型參數(shù);
// 而在普通函數(shù)調(diào)用中,僅憑返回值或賦值目標(biāo)無(wú)法反推出泛型參數(shù)的具體類(lèi)型。
/**
? 場(chǎng)景一:屬性委托 + reified 泛型函數(shù) ? 可以推斷
private val binding: FragmentDelegateBinding by viewBindingV1()
? 場(chǎng)景二:普通函數(shù)調(diào)用 ? 無(wú)法推斷
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í)省略參數(shù)的傳遞,Kotlin會(huì)自動(dòng)根據(jù)泛型類(lèi)型幫你找到對(duì)應(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),第一個(gè)參數(shù)為null(因?yàn)槭庆o態(tài)方法) 第二個(gè)參數(shù)是View,來(lái)自Fragment的requireView()
* as T 將結(jié)果強(qiáng)制轉(zhuǎn)換為泛型T,即具體的ViewBinding子類(lèi)(如FragmentDelegateBinding)
* FragmentDelegateBinding.bind(Fragment.requireView())
*/
bindMethod.invoke(null,fragment.requireView()) as T
}
}
/**
* viewBinder 創(chuàng)建綁定類(lèi)對(duì)象
*/
class FragmentViewBindingProperttV1<in F:Fragment,out V: ViewBinding>(
private val viewBinder:(F) ->V //給定一個(gè) Fragment(或其子類(lèi)),返回一個(gè)對(duì)應(yīng)的 ViewBinding 實(shí)例
):ReadOnlyProperty<F,V>{
private var viewBinding:V? = null
override fun getValue(thisRef: F, property: KProperty<*>): V {
//viewBinding不為空說(shuō)明已經(jīng)綁定,直接返回
viewBinding?.let {
return it
}
//Fragment視圖的生命周期
val lifecycle = thisRef.viewLifecycleOwner.lifecycle
//實(shí)例化綁定類(lèi)對(duì)象
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ù)的主要目的是獲得一個(gè)ViewBinding綁定類(lèi)對(duì)象,我們可以把創(chuàng)建對(duì)象的行為交給外部去定義
inline fun <F:Fragment,V:ViewBinding> viewBindingV2(
crossinline viewBinder:(View) -> V,//接受一個(gè)View,返回Binding實(shí)例
crossinline viewProvider:(F) -> View = {
fragment -> fragment.requireView() //這里的fragment就是F
} //接受一個(gè)Fragment,返回它的View
) = FragmentViewBindingPropertyV2{ fragment:F ->
viewBinder(viewProvider(fragment)) //FragmentDelegateBinding.bind(Fragment.requireView())
}//fragment它是 Kotlin 屬性委托機(jī)制在訪問(wèn) binding 屬性時(shí)自動(dòng)傳入的當(dāng)前 Fragment 實(shí)例
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不為空說(shuō)明已經(jīng)綁定,直接返回
viewBinding?.let {
return it
}
//Fragment視圖的生命周期
val lifecycle = thisRef.viewLifecycleOwner.lifecycle
//實(shí)例化綁定類(lèi)對(duì)象
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委托機(jī)制使用方式和原理的文章就介紹到這了,更多相關(guān)Kotlin委托內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JSON字符串轉(zhuǎn)成java的Map對(duì)象詳細(xì)步驟
這篇文章主要介紹了如何將JSON字符串轉(zhuǎn)換為Java對(duì)象的步驟,包括定義Element類(lèi)、使用Jackson庫(kù)解析JSON和添加依賴(lài),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
IDEA啟動(dòng)tomcat控制臺(tái)中文亂碼問(wèn)題的解決方法(100%有效)
很多人在idea中啟動(dòng)項(xiàng)目時(shí)會(huì)出現(xiàn)控制臺(tái)的中文亂碼,其實(shí)也無(wú)傷大雅,但是本人看著不舒服,下面這篇文章主要給大家介紹了關(guān)于IDEA啟動(dòng)tomcat控制臺(tái)中文亂碼問(wèn)題的解決方法,需要的朋友可以參考下2022-09-09
logback標(biāo)記日志過(guò)濾器MarkerFilter源碼解讀
這篇文章主要為大家介紹了logback標(biāo)記日志過(guò)濾器MarkerFilter源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
SpringBoot整合Druid數(shù)據(jù)源過(guò)程詳解
這篇文章主要介紹了SpringBoot整合Druid數(shù)據(jù)源過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
java圖片和文本同時(shí)提交到表單的實(shí)例代碼
在本篇文章里小編給大家整理的是關(guān)于java實(shí)現(xiàn)圖片和文本同時(shí)提交到表單的相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-02-02
Java中回調(diào)函數(shù)?(callback)?及其實(shí)際應(yīng)用場(chǎng)景
在Java中回調(diào)函數(shù)(Callback)是一種常見(jiàn)的設(shè)計(jì)模式,用于實(shí)現(xiàn)異步操作或事件處理,這篇文章主要給大家介紹了關(guān)于Java中回調(diào)函數(shù)?(callback)?及其實(shí)際應(yīng)用場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2024-02-02
解讀為什么@Autowired在屬性上被警告,在setter方法上不被警告問(wèn)題
在Spring開(kāi)發(fā)中,@Autowired注解常用于實(shí)現(xiàn)依賴(lài)注入,它可以應(yīng)用于類(lèi)的屬性、構(gòu)造器或setter方法上,然而,當(dāng)@Autowired注解在屬性上使用時(shí),IntelliJIDEA等IDE會(huì)給出Fieldinjectionisnotrecommended的警告,而在setter方法上使用@Autowired時(shí)卻不會(huì)出現(xiàn)這個(gè)警告2025-02-02

