2017-08-14 21:23:34

一个较为复杂的Parcelable实现类

  1 public class CommentShareBean implements Parcelable {
  2         /**
  3          * 提示文案
  4          */
  5         public String shareTitle;
  6         /**
  7          * 商家名称
  8          */
  9         public String poiName;
 10         /**
 11          * 商家icon
 12          */
 13         public String poiIcon;
 14         /**
 15          * 订单评分
 16          */
 17         public int commentScore;
 18         /**
 19          * 评论
 20          */
 21         public String commentContent;
 22         /**
 23          *  赞商品
 24          */
 25         public List<PraiseFood> praiseFoodList;
 26         /**
 27          * 生成二维码的url
 28          */
 29         public String poiShareUrl;
 30         /**
 31          * slogan(icon+文案)
 32          */
 33         public String mtSlogan;
 34         /**
 35          * 二维码分享文案
 36          */
 37         public String urlTip;
 38         /**
 39          * 可分享 道[1,2] 1:微信朋友圈 2:微信好友 3:qq空间 4:qq好友
 40          */
 41         public List<Integer> channels;
 42 
 43         public CommentShareBean() {
 44 
 45         }
 46 
 47         private CommentShareBean(Parcel in) {
 48             shareTitle = in.readString();
 49             poiName = in.readString();
 50             poiIcon = in.readString();
 51             commentScore = in.readInt();
 52             commentContent = in.readString();
 53             if (in.readByte() == (byte) 1) {
 54                 PraiseFood[] foods = in.createTypedArray(PraiseFood.CREATOR);
 55                 praiseFoodList = Arrays.asList(foods);
 56             }
 57             poiShareUrl = in.readString();
 58             mtSlogan = in.readString();
 59             urlTip = in.readString();
 60             if (in.readByte() == (byte) 1) {
 61                 int[] chas = in.createIntArray();
 62                 channels = new ArrayList<>();
 63                 for (int i = 0; i < chas.length; i++) {
 64                     channels.add(chas[i]);
 65                 }
 66             }
 67         }
 68 
 69         @Override
 70         public int describeContents() {
 71             return 9887;
 72         }
 73 
 74         @Override
 75         public void writeToParcel(Parcel dest, int flags) {
 76             dest.writeString(shareTitle);
 77             dest.writeString(poiName);
 78             dest.writeString(poiIcon);
 79             dest.writeInt(commentScore);
 80             dest.writeString(commentContent);
 81             if (praiseFoodList != null && praiseFoodList.size() > 0) {
 82                 dest.writeByte((byte) 1);
 83                 PraiseFood[] ps = new PraiseFood[praiseFoodList.size()];
 84                 for (int i = 0; i < praiseFoodList.size(); i++) {
 85                     ps[i] = praiseFoodList.get(i);
 86                 }
 87                 dest.writeTypedArray(ps, 0);
 88             } else {
 89                 dest.writeByte((byte) 0);
 90             }
 91             dest.writeString(poiShareUrl);
 92             dest.writeString(mtSlogan);
 93             dest.writeString(urlTip);
 94             if (channels != null && channels.size() > 0) {
 95                 dest.writeByte((byte) 1);
 96                 int[] chas = new int[channels.size()];
 97                 for (int i = 0; i < channels.size(); i++) {
 98                     chas[i] = channels.get(i);
 99                 }
100                 dest.writeIntArray(chas);
101             } else {
102                 dest.writeByte((byte) 0);
103             }
104         }
105 
106         public static final Creator<CommentShareBean> CREATOR = new Creator<CommentShareBean>() {
107             @Override
108             public CommentShareBean createFromParcel(Parcel source) {
109                 return new CommentShareBean(source);
110             }
111 
112             @Override
113             public CommentShareBean[] newArray(int size) {
114                 return new CommentShareBean[size];
115             }
116         };
117 
118         public static class PraiseFood implements Parcelable {
119             public String foodName;
120 
121             public PraiseFood(String name) {
122                 foodName = name;
123             }
124 
125             public PraiseFood(Parcel in) {
126                 foodName = in.readString();
127             }
128 
129             @Override
130             public int describeContents() {
131                 return 12345;
132             }
133 
134             @Override
135             public void writeToParcel(Parcel dest, int flags) {
136                 dest.writeString(foodName);
137             }
138 
139             public static final Parcelable.Creator<PraiseFood> CREATOR = new Parcelable.Creator<PraiseFood>() {
140 
141                 @Override
142                 public PraiseFood createFromParcel(Parcel source) {
143                     return new PraiseFood(source);
144                 }
145 
146                 @Override
147                 public PraiseFood[] newArray(int size) {
148                     return new PraiseFood[size];
149                 }
150             };
151         }
152     }

 

 posted on 2017-08-14 21:26  wlrhnh  阅读(982)  评论(0编辑  收藏  举报