• kotlin


    本文摘自——https://kotlinlang.org/docs/tutorials/android-plugin.html

    Android Extensions plugin provides Parcelable implementation generator as an experimental feature. To be able to use it, turn on the experimental flag.

    How to use

    Annotate the class with @Parcelize, and a Parcelable implementation will be generated automatically.

    import kotlinx.android.parcel.Parcelize
    @Parcelize
    class User(val firstName: String, val lastName: String, val age: Int): Parcelable

    @Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, @Parcelize can't be applied if some of the primary constructor parameters are not properties.

    If your class requires more advanced serialization logic, you can write it inside a companion class:

    @Parcelize
    data class User(val firstName: String, val lastName: String, val age: Int) : Parcelable {
        private companion object : Parceler<User> {
            override fun User.write(parcel: Parcel, flags: Int) {
                // Custom write implementation
            }
    
            override fun create(parcel: Parcel): User {
                // Custom read implementation
            }
        }
    }

    Supported types

    @Parcelize supports a wide range of types:

    • primitive types (and their boxed versions);
    • objects and enums;
    • StringCharSequence;
    • Exception;
    • SizeSizeFBundleIBinderIInterfaceFileDescriptor;
    • SparseArraySparseIntArraySparseLongArraySparseBooleanArray;
    • all Serializable (yes, Date is supported too) and Parcelable implementations;
    • collections of all supported types: List (mapped to ArrayList), Set (mapped to LinkedHashSet), Map (mapped to LinkedHashMap);
      • Also a number of concrete implementations: ArrayListLinkedListSortedSetNavigableSetHashSetLinkedHashSetTreeSetSortedMapNavigableMapHashMapLinkedHashMapTreeMapConcurrentHashMap;
    • arrays of all supported types;
    • nullable versions of all supported types.

    Custom Parcelers

    Even if your type is not supported directly, you can write a Parceler mapping object for it.

    class ExternalClass(val value: Int)
    
    object ExternalClassParceler : Parceler<ExternalClass> {
        override fun create(parcel: Parcel) = ExternalClass(parcel.readInt())
    
        override fun ExternalClass.write(parcel: Parcel, flags: Int) {
            parcel.writeInt(value)
        }
    }

    External parcelers can be applied using @TypeParceler or @WriteWith annotations:

     
    // Class-local parceler
    @Parcelize
    @TypeParceler<ExternalClass, ExternalClassParceler>()
    class MyClass(val external: ExternalClass)
    
    // Property-local parceler
    @Parcelize
    class MyClass(@TypeParceler<ExternalClass, ExternalClassParceler>() val external: ExternalClass)
    
    // Type-local parceler
    @Parcelize
    class MyClass(val external: @WriteWith<ExternalClassParceler>() ExternalClass)
     
  • 相关阅读:
    BZOJ_2460_[BeiJing2011]元素_线性基
    BZOJ_4448_[Scoi2015]情报传递_主席树
    BZOJ_4004_[JLOI2015]装备购买_线性基
    BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组
    BZOJ_4128_Matrix_矩阵乘法+哈希+BSGS
    BZOJ_4378_[POI2015]Logistyka_树状数组
    BZOJ_2527_[Poi2011]Meteors_整体二分
    BZOJ_2738_矩阵乘法_整体二分
    BZOJ_3687_简单题_bitset
    HDU 4501
  • 原文地址:https://www.cnblogs.com/heweiquan/p/11082452.html
Copyright © 2020-2023  润新知