目前,我仍然想知道当我们为一个类实现Parcelable
接口时,我们如何决定何时使用writeTypedList/ readTypedList
或writeList/ readList
朗读
// Using writeTypedList
parcel.writeTypedList(watchlistColumnTypes);
// Using writeList
parcel.writeList(watchlistColumnTypes);
写
// Using readTypedList
watchlistColumnTypes = new ArrayList<>();
in.readTypedList(watchlistColumnTypes, WatchlistArrayRecyclerViewAdapter.ColumnType.CREATOR);
// Using readList
watchlistColumnTypes = new ArrayList<>();
in.readList(watchlistColumnTypes, WatchlistArrayRecyclerViewAdapter.class.getClassLoader());
对我来说都是可行的。但是,我不知道两者之间有什么区别,我们如何在两者之间作出选择呢?
发布于 2017-08-10 12:16:57
我认为经验法则是,我们应该尽可能使用writeTypedList
或readTypedList
,并在需要时使用writeList
或readList
。
这是因为writeTypedList
或readTypedList
似乎具有更好的性能。
writeTypedObject(T,int)、writeTypedArray(T[],int)、writeTypedList(List)、readTypedObject(Parcelable.Creator)、createTypedArray(Parcelable.Creator)和createTypedArrayList(Parcelable.Creator)也提供了一种更有效的处理Parcelable的方法。这些方法不写入原始对象的类信息:相反,read函数的调用方必须知道预期的类型并传入适当的Parcelable.Creator,以正确构造新对象并读取其数据。
https://stackoverflow.com/questions/45267609
复制相似问题