Android Parcelable自动生成插件

763 阅读1分钟
  1. 安装插件 Android Parcelable Code Generator

截图.png

  1. 创建数据类Rect, 右键,选择Generate -> Parcelable

截图 (1).png

截图 (3).png

截图 (2).png

  1. 生成效果如下
import android.os.Parcel;
import android.os.Parcelable;

public final class Rect implements Parcelable {
    public int left;
    public int top;
    public int right;
    public int bottom;
    
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.left);
        dest.writeInt(this.top);
        dest.writeInt(this.right);
        dest.writeInt(this.bottom);
    }

    public void readFromParcel(Parcel source) {
        this.left = source.readInt();
        this.top = source.readInt();
        this.right = source.readInt();
        this.bottom = source.readInt();
    }

    public Rect() {
    }

    protected Rect(Parcel in) {
        this.left = in.readInt();
        this.top = in.readInt();
        this.right = in.readInt();
        this.bottom = in.readInt();
    }

    public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() {
        @Override
        public Rect createFromParcel(Parcel source) {
            return new Rect(source);
        }

        @Override
        public Rect[] newArray(int size) {
            return new Rect[size];
        }
    };
}