kotlin - Parcelable implementations generator
本文摘自——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;
String
,CharSequence
;Exception
;Size
,SizeF
,Bundle
,IBinder
,IInterface
,FileDescriptor
;SparseArray
,SparseIntArray
,SparseLongArray
,SparseBooleanArray
;- all
Serializable
(yes,Date
is supported too) andParcelable
implementations; - collections of all supported types:
List
(mapped toArrayList
),Set
(mapped toLinkedHashSet
),Map
(mapped toLinkedHashMap
);- Also a number of concrete implementations:
ArrayList
,LinkedList
,SortedSet
,NavigableSet
,HashSet
,LinkedHashSet
,TreeSet
,SortedMap
,NavigableMap
,HashMap
,LinkedHashMap
,TreeMap
,ConcurrentHashMap
;
- Also a number of concrete implementations:
- arrays of all supported types;
- nullable versions of all supported types.
Custom Parceler
s
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)
kotlin - Parcelable implementations generator的更多相关文章
- Android项目实战(十九):Android Studio 优秀插件: Parcelable Code Generator
Android Studio 优秀插件系列: Android Studio 优秀插件(一):GsonFormat Android Studio 优秀插件(二): Parcelable Code Gen ...
- Android Studio 优秀插件: Parcelable Code Generator
这里假设我们已经会使用 Parcelable 序列化一个对象了~~ 那么大家会发现 Parcelable 使用起来有些复杂,因为我们要自己复写 几个方法,而且当类的属性比较多的时候,我们就会难受了,又 ...
- Android Studio酷炫插件(一)——自动化快速实现Parcelable接口序列化
https://blog.csdn.net/kroclin/article/details/40902721 一.前言相信数据序列化大家都多多少少有接触到,比如自定义了一个实体类,需要在activit ...
- 服务 AIDL 定向tag IPC Parcelable 案例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Android Studio自动化快速实现Parcelable接口序列化
1.在线安装 然后打开File -> Settings -> Pugins -> Browse Repositories 如下,输入android parcelable code g ...
- Android Studio各种快捷功能及好用的插件
转载:http://www.jianshu.com/p/c439605a4364 概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的, ...
- C++ 下面的AIDL
转自https://android.googlesource.com/platform/system/tools/aidl/+/brillo-m10-dev/docs/aidl-cpp.md. Bac ...
- Android Weekly Notes Issue #257
Android Weekly Issue #257 May 14th, 2017 Android Weekly Issue #257 本期内容包括: Gradle中关于项目的一些设置; Android ...
- [Android Tips] 19. Android Studio Plugins
Code Generation GsonFormat json 字符串生成实体类 https://github.com/zzz40500/GsonFormat Android Parcelable C ...
随机推荐
- E20180327-hm
renew vt. 补充; 重新开始; 使更新; 使恢复; vi. 重申,重复强调; 重新开始; renewal n. 重建,重生; 更新,革新; 重申; 合同的续订;
- IOS高级开发~Runtime(一)
#import <Foundation/Foundation.h> @interface CustomClass : NSObject -(void)fun1; @end @interfa ...
- 当打开一个.h或.cpp文件时, Solution Explorer就自动展开文件所在的目录
当打开一个.h或.cpp文件时, Solution Explorer就自动展开文件所在的目录: 如果不想展开: Tools -> Options -&g ...
- 语句 if
1.if语句单项分支 #单项分支 if 55 < 133: print('这个比较运算是正确的') #输出结果:这个比较运算是正确的 2.if语句双项分支 #双项分支 if 55 > 14 ...
- 运行scrapyd报错
转自: https://blog.csdn.net/qq_29719097/article/details/89431234 web.Server Traceback (most recent ...
- 连接php/mysql
1 安装php5.6 首先确保mysql已安装 ./configure --prefix=/app/php-5.6.36 --with-mysql=mysqlnd --enable-mysqlnd - ...
- Alt+数字 输入特殊字符
前言: 按住Alt键不放,再按(小键盘的)数字键,然后放开就可以输入特殊字符. 起始 终止 字符类别 0 255 基本与ASCII 码表对应 42657 42680 大写希腊字母 ...
- adb logcat教程
1.速查 $adb logcat -g //打印和缓冲区使用情况 $adb logcat -c main //清除main缓存区 $adb logcat -v threadtime -f /data/ ...
- java 环境变量的设置,备忘
新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1.7.0变量名:CLASSPATH 变量值:.;%JAVA ...
- Aria's Loops
https://www.hackerrank.com/contests/101hack41/challenges/arias-loops 可以看我以前的笔记,http://www.cnblogs.co ...