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,Dateis supported too) andParcelableimplementations; - 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 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)
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 ...
随机推荐
- Unity3D4.* NGUI制作动态字库
新建一个工程,这个工程必须没有中文路径,否则会不识别字体!!! 首先导入NGUI插件,这里我用的是NGUI 3.0.2版本的. 在Assets 下创建一个文件夹,用来存放接下来的工作文件 . 这里随便 ...
- VMware ESXI虚拟机挂载移动硬盘
Windows server2008 R2 1.“编辑虚拟机设置”,点击“添加” 2.点击添加“USB控制器”: 3.添加完“USB控制器”以后,再点击添加“USB设备”: 完成即可:如果系统里面不显 ...
- python接口自动化(三十八)-python操作mysql数据库(详解)
简介 现在的招聘要求对QA人员的要求越来越高,测试的一些基础知识就不必说了,来说测试知识以外的,会不会一门或者多门开发与语言,能不能读懂代码,会不会Linux,会不会搭建测试系统,会不会常用的数据库, ...
- windows 远程连接 密码正确但是无法登陆,提示证书不正确
问题: windows8.1 远程连接 windows8 进入输入用户名密码环节,用户名,密码都正确,但是无法登陆 连接时,下方写着 域名 MicrosoftAccount 解决: 在输入密码后,点击 ...
- python 之 random 模块、 shutil 模块、shelve模块、 xml模块
6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大 ...
- 7天学完Java基础之4/7
静态static 如果一个成员变量使用了static关键字,那么这个变量不再属于对象自己,而是属于所在的类,多个对象共享同一份数据 静态static 关键字修饰成员变量 public class St ...
- notepad++插件选项没有plugin manager解决
在 https://github.com/bruderstein/nppPluginManager/releases 下载最新的PluginManager_vXXXX_UNI.zip 解压,将里面的p ...
- 基于Tcp协议的上传下载
目录格式: 构建此目录就可随意使用! client端 import socket import sys import os import json import struct sk = socket. ...
- Calculation 2 HDU - 3501
https://vjudge.net/problem/HDU-3501 不会做啊...记一下做法 做法是计算小于n且与n互质的数的和:根据如果gcd(i,n)==1,那么gcd(n-i,n)==1,对 ...
- 题解报告:hdu 4135 Co-prime(容斥定理入门)
Problem Description Given a number N, you are asked to count the number of integers between A and B ...