-
-
Save Hasiy/71685c0665ee4a159443ffc2bd3db8a3 to your computer and use it in GitHub Desktop.
Fragment arguments 使用kotlin 委托传值
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
package com.example.myapplication | |
import android.os.Binder | |
import android.os.Bundle | |
import androidx.core.app.BundleCompat | |
import androidx.fragment.app.Fragment | |
import kotlin.properties.ReadWriteProperty | |
import kotlin.reflect.KProperty | |
/** | |
* Eases the Fragment.newInstance ceremony by marking the fragment's args with this delegate | |
* Just write the property in newInstance and read it like any other property after the fragment has been created | |
*/ | |
class FragmentArgumentDelegate<T : Any> : ReadWriteProperty<Fragment, T> { | |
override operator fun getValue(thisRef: Fragment, property: KProperty<*>): T { | |
val args = thisRef.arguments ?: throw IllegalStateException("Cannot read property ${property.name} if no arguments have been set") | |
@Suppress("UNCHECKED_CAST") | |
return args.get(property.name) as T? ?: throw IllegalStateException("Property ${property.name} could not be read") | |
} | |
override operator fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) { | |
if (thisRef.arguments == null) { | |
thisRef.arguments = Bundle() | |
} | |
val args = thisRef.arguments!! | |
val key = property.name | |
when (value) { | |
is Boolean -> args.putBoolean(key, value) | |
is BooleanArray -> args.putBooleanArray(key, value) | |
is String -> args.putString(key, value) | |
is Int -> args.putInt(key, value) | |
is Short -> args.putShort(key, value) | |
is Long -> args.putLong(key, value) | |
is Byte -> args.putByte(key, value) | |
is ByteArray -> args.putByteArray(key, value) | |
is Char -> args.putChar(key, value) | |
is CharArray -> args.putCharArray(key, value) | |
is CharSequence -> args.putCharSequence(key, value) | |
is Float -> args.putFloat(key, value) | |
is Bundle -> args.putBundle(key, value) | |
is Binder -> BundleCompat.putBinder(args, key, value) | |
is android.os.Parcelable -> args.putParcelable(key, value) | |
is java.io.Serializable -> args.putSerializable(key, value) | |
else -> throw IllegalStateException("Type ${value.javaClass.canonicalName} of property ${property.name} is not supported") | |
} | |
} | |
} |
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
package com.example.myapplication | |
import android.os.Bundle | |
import androidx.widget.Toast | |
/** | |
* Example usage of FragmentArgumentDelegate | |
*/ | |
class WeatherCityFragment : Fragment() { | |
private var cityId : String by FragmentArgumentDelegate() | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
Toast.makeText(activity, cityId, Toast.LENGTH_SHORT).show() | |
} | |
companion object { | |
fun instance(cityId: String) = WeatherCityFragment().apply { | |
this.cityId = cityId | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment