编辑:
利用Type Parcelers和@WriteWith
注释的功能,可以创建一个RealmList类型的Parceler,它可以帮助你处理这种场景:
使用以下代码:
fun <T> Parcel.readRealmList(clazz: Class<T>): RealmList<T>?
where T : RealmModel,
T : Parcelable = when {
readInt() > 0 -> RealmList<T>().also { list ->
repeat(readInt()) {
list.add(readParcelable(clazz.classLoader))
}
}
else -> null
}
fun <T> Parcel.writeRealmList(realmList: RealmList<T>?, clazz: Class<T>)
where T : RealmModel,
T : Parcelable {
writeInt(when {
realmList == null -> 0
else -> 1
})
if (realmList != null) {
writeInt(realmList.size)
for (t in realmList) {
writeParcelable(t, 0)
}
}
}
您可以像这样定义一个接口:
interface RealmListParceler<T>: Parceler<RealmList<T>?> where T: RealmModel, T: Parcelable {
override fun create(parcel: Parcel): RealmList<T>? = parcel.readRealmList(clazz)
override fun RealmList<T>?.write(parcel: Parcel, flags: Int) {
parcel.writeRealmList(this, clazz)
}
val clazz : Class<T>
}
您需要为 RealmList<Car>
创建一个特定的解析器,例如:
object CarRealmListParceler: RealmListParceler<Car> {
override val clazz: Class<Car>
get() = Car::class.java
}
但是现在,你可以做到以下几点:
@Parcelize
@RealmClass
open class Garage(
var name: String? = null,
var cars: @WriteWith<CarRealmListParceler> RealmList<Car> = RealmList()
) : Parcelable, RealmModel
And
@Parcelize
@RealmClass
open class Car(
..
): RealmModel, Parcelable {
...
}
这样你就不需要手动编写Parceler逻辑。
原始回答:
以下应该可以工作:
@Parcelize
open class Garage: RealmObject(), Parcelable {
var name: String? = null
var cars: RealmList<Car>? = null
companion object : Parceler<Garage> {
override fun Garage.write(parcel: Parcel, flags: Int) {
parcel.writeNullableString(name)
parcel.writeRealmList(cars)
}
override fun create(parcel: Parcel): Garage = Garage().apply {
name = parcel.readNullableString()
cars = parcel.readRealmList()
}
}
}
只要您也添加以下扩展功能:
inline fun <reified T> Parcel.writeRealmList(realmList: RealmList<T>?)
where T : RealmModel,
T : Parcelable {
writeInt(when {
realmList == null -> 0
else -> 1
})
if (realmList != null) {
writeInt(realmList.size)
for (t in realmList) {
writeParcelable(t, 0)
}
}
}
inline fun <reified T> Parcel.readRealmList(): RealmList<T>?
where T : RealmModel,
T : Parcelable = when {
readInt() > 0 -> RealmList<T>().also { list ->
repeat(readInt()) {
list.add(readParcelable(T::class.java.classLoader))
}
}
else -> null
}
同时也
fun Parcel.writeNullableString(string: String?) {
writeInt(when {
string == null -> 0
else -> 1
})
if (string != null) {
writeString(string)
}
}
fun Parcel.readNullableString(): String? = when {
readInt() > 0 -> readString()
else -> null
}
Garage.write
,否则你就要创建一个新的实例(我之前有一次复制粘贴错误)- 但是我刚刚意识到,我可能还可以使用Parceler<RealmList<T>>
和@WriteWith<RealmListParceler>()
来改进这个答案 - 假设我能让它的泛型工作,无论如何。 - EpicPandaForce