Android项目实战(十九):Android Studio 优秀插件: Parcelable Code Generator
Android Studio 优秀插件系列:
Android Studio 优秀插件(一):GsonFormat
Android Studio 优秀插件(二): Parcelable Code Generator
-----------------------------------------------------------------------------
Parcelable , 这个词大家应该不陌生吧,用于序列化对象的一个接口
不清楚的可以看一下这篇博客:Intent传递对象的两种方法
-----------------------------------------------------------------------------
这里假设我们已经会使用 Parcelable 序列化一个对象了~~
那么大家会发现 Parcelable 使用起来有些复杂,因为我们要自己复写 几个方法,而且当类的属性比较多的时候,我们就会难受了,又要注意不写错属性名,又要注意写对属性的类型,又要花不少的时间做重复的事情。
那么因为 Parcelable 有使用它的优势,我们又不能放弃,那该怎么办么?
Android Studio 提供给了我们 一个插件用来简化 给一个类 实现 Parcelable 接口的流程。
-----------------------------------------------------------------------------
现在学习下如何使用这个插件:
1、Android Studio 打开一个项目,点击左上角 File -->Settings... 进行设置
2、选择插件Plugins , 搜索Parcel,如果你没有下载过这个插件,那么搜索框下面会显示“Nothing to show.Click Browse to....”
3、那就点击蓝色字体的 Browse 吧 ,这个时候会出现如下图的界面,我们只需要在左边选中arcel然后点击右面 绿色按钮 "Install plugin" 就可以了
4、完成了上面三个步骤,就可以使用Parcelable Code Generator插件了
怎么用呢,
(1)创建一个类文件,类名是看你需求自定义写的,添加上你需要的属性
(2)快捷键 alt+insert ,会出现如下选择框,选择Parcelable 即可
然后我们就看到代码,是不是比我们手动写要快的许多
public class People implements Parcelable { private int id; private String url; private int width; private int height; private int likeCount; private String description; private int time; private int replyCount; private int floorCount; private int likeUserCount; private int age; private String name; private String school; private int type; private String sax; private int userid; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.id); dest.writeString(this.url); dest.writeInt(this.width); dest.writeInt(this.height); dest.writeInt(this.likeCount); dest.writeString(this.description); dest.writeInt(this.time); dest.writeInt(this.replyCount); dest.writeInt(this.floorCount); dest.writeInt(this.likeUserCount); dest.writeInt(this.age); dest.writeString(this.name); dest.writeString(this.school); dest.writeInt(this.type); dest.writeString(this.sax); dest.writeInt(this.userid); } public People() { } protected People(Parcel in) { this.id = in.readInt(); this.url = in.readString(); this.width = in.readInt(); this.height = in.readInt(); this.likeCount = in.readInt(); this.description = in.readString(); this.time = in.readInt(); this.replyCount = in.readInt(); this.floorCount = in.readInt(); this.likeUserCount = in.readInt(); this.age = in.readInt(); this.name = in.readString(); this.school = in.readString(); this.type = in.readInt(); this.sax = in.readString(); this.userid = in.readInt(); } public static final Parcelable.Creator<People> CREATOR = new Parcelable.Creator<People>() { public People createFromParcel(Parcel source) { return new People(source); } public People[] newArray(int size) { return new People[size]; } }; }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android开发实战(十八):Android Studio 优秀插件:GsonFormat
原文: Android开发实战(十八):Android Studio 优秀插件:GsonFormat Android Studio 优秀插件系列: Android Studio 优秀插件(一):GsonFormat Android Studio 优秀插件(二): Parcelable Code Generator ------------------------------------------------------------------------------------------------------- 这几天没有活,于是乎整理了一些代码,顺便把一些一直在使用的东西也整理下,然后学习新的知识。。 ------------------------------------------------------------------------------------------------------- 作为一个Android程序猿,当你看到后台给你的json数据格式时: { "id":123, "url": "http://img....
- 下一篇
Android项目实战(二十):浅谈ListView悬浮头部展现效果
原文: Android项目实战(二十):浅谈ListView悬浮头部展现效果 先看下效果:需求是 滑动列表 ,其中一部分视图(粉丝数,关注数这一部分)在滑动到顶端的时候不消失,而是停留在整个界面头部。 我们先分析要解决的问题: 1、如何实现列表ListView顶部视图跟随ListView一起滑动 2、如何实现滑动过程中需要停留在顶部的视图 解决: 第一个问题,实现ListView与顶部视图一起滑动,ListView提供一个方法,addHeadView(View); 意思就是在ListView顶部添加一个View。那么这个View就能和ListView一起滚动。 第二个问题,怎么保证界面中间的某一部分视图滑动到顶部的时候停留在顶部呢? 首先我们这个停留在顶部的View(称为View1)是ListView.addHeadView()上去的,即滑动列表,这个View1会划出去,那么如何让它不划出去?只要在Listview所在布局最上方 也写一个同样的View(称为View2,View2和ListView同属于一个FragmentLayout)并先隐藏(Visible = 'gone'),...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果