Android项目实战(十九):Android Studio 优秀插件: Parcelable Code Generator

Android Studio 优秀插件系列:html

                      Android Studio 优秀插件(一):GsonFormatide

                      Android Studio 优秀插件(二): Parcelable Code Generator

 

-----------------------------------------------------------------------------post

Parcelable  , 这个词你们应该不陌生吧,用于序列化对象的一个接口学习

不清楚的能够看一下这篇博客:Intent传递对象的两种方法字体

-----------------------------------------------------------------------------this

这里假设咱们已经会使用 Parcelable 序列化一个对象了~~url

那么你们会发现 Parcelable 使用起来有些复杂,由于咱们要本身复写 几个方法,并且当类的属性比较多的时候,咱们就会难受了,又要注意不写错属性名,又要注意写对属性的类型,又要花很多的时间作重复的事情。spa

 

那么由于 Parcelable 有使用它的优点,咱们又不能放弃,那该怎么办么?插件

Android Studio 提供给了咱们 一个插件用来简化 给一个类 实现 Parcelable 接口的流程。code

 

-----------------------------------------------------------------------------

如今学习下如何使用这个插件:

 

一、Android Studio 打开一个项目,点击左上角 File -->Settings... 进行设置

 

 

 

 

二、选择插件Plugins , 搜索Parcel,若是你没有下载过这个插件,那么搜索框下面会显示“Nothing to show.Click Browse to....”

 

 

 

 

 

三、那就点击蓝色字体的 Browse 吧  ,这个时候会出现以下图的界面,咱们只须要在左边选中arcel而后点击右面 绿色按钮 "Install plugin" 就能够了

 

 

 

 

四、完成了上面三个步骤,就可使用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];
        }
    };
}
相关文章
相关标签/搜索