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];
        }
    };
}
相關文章
相關標籤/搜索