提高效率:Android Parcelables,接口和列表

时间:2013-06-10 15:54:47

标签: android parcelable

问题是:如何在实现接口的parcelables上有效地实现parceling。

我有这个案子。由两个具体类(参见下图)和一个应该是Parcelable(PictureProviders)的容器实现的接口。

问题是:如何有效地包裹图片列表?

  1. 我们不能使用read / writeTypedList,因为我们不知道要传递给readTypedList的创建者。
  2. 我们可以使用read / writeParcelableArray,但这意味着将List复制到数组。你还会使用哪个类加载器? getClass()。getClassLoader()可以吗?
  3. 我们也可以使用read / writeList。你会使用哪个类加载器? getClass()。getClassLoader()可以吗?
  4. 示例代码:

    public interface PictureProvider extends Parcelable {
      public Collection<Picture> getAvailablePictures(); 
    }
    
    public class SimplePictureProvider implements PictureProvider {
    
      // ...
    
      private List<Picture> pictures;
    
      public SimplePictureProvider(Parcel in) {
        // THIS IS WHERE THE QUESTION APPLIES
      }
    
      @Override
      public void writeToParcel(Parcel dest, int flags) {
        // THIS IS WHERE THE QUESTION APPLIES
      }
    
      public int describeContents() {
        return 0;
      }
    
      public static final Parcelable.Creator<SimplePictureProvider> CREATOR = new Parcelable.Creator<SimplePictureProvider>() {
        public SimplePictureProvider createFromParcel(Parcel in) {
          return new SimplePictureProvider(in);
        }
    
        public SimplePictureProvider[] newArray(int size) {
          return new SimplePictureProvider[size];
        }
      };
    }
    
    public interface Picture extends Parcelable {
      public byte[] getPictureData();
    }
    
    public class RemotePicture implements Picture {
    
      // ...
    
      private String pictureUrl;
    
      public RemotePicture(Parcel in) {
        pictureUrl = in.readString();
      }
    
      @Override
      public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(pictureUrl);
      }
    
      public int describeContents() {
        return 0;
      }
    
      public static final Parcelable.Creator<RemotePicture> CREATOR = new Parcelable.Creator<RemotePicture>() {
        public RemotePicture createFromParcel(Parcel in) {
          return new RemotePicture(in);
        }
    
        public RemotePicture[] newArray(int size) {
          return new RemotePicture[size];
        }
      };
    }
    
    public class LocalPicture implements Picture {
    
      // ...
    
      private String picturePath;
    
      public LocalPicture(Parcel in) {
        picturePath = in.readString();
      }
    
      @Override
      public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(picturePath);
      }
    
      public int describeContents() {
        return 0;
      }
    
      public static final Parcelable.Creator<LocalPicture> CREATOR = new Parcelable.Creator<LocalPicture>() {
        public LocalPicture createFromParcel(Parcel in) {
          return new LocalPicture(in);
        }
    
        public LocalPicture[] newArray(int size) {
          return new LocalPicture[size];
        }
      };
    }
    

2 个答案:

答案 0 :(得分:0)

我会选择writeParcelableArray,如果您知道尺寸不会改变,*List如果尺寸动态变化。将null作为类加载器传递以使用基本/默认类加载器。

答案 1 :(得分:0)

我使用read / writeList和getClass()。getClassLoader()?这似乎可以完成这项工作。