我的应用程序中的第一个活动有三个选项卡第一个选项卡包括包含图像列表的回收视图,这些图像来自webservice,当单击第二个或第三个选项卡然后单击选项卡第一个图像从WebService加载我想要为片段
保存实例答案 0 :(得分:0)
首先,在您的回收站视图的适配器中使用Picasso库来显示图像。它具有开箱即用的图像缓存。当您第一次下载图像时,它将被缓存在您的设备上。当您尝试再次使用相同的URL下载图像时(从第三个选项卡返回时)毕加索将从缓存中显示图像,并且不会再次下载。
其次,您可以将图像列表保存在片段中。在你的片段覆盖中
String BUNDLE_IMAGES = "imgs";
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
ourState.putStringArrayList(BUNDLE_IMAGES , getImageArray())
}
private List<String> getImageArray(){
//your implementation to get image array
}
恢复
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
List<String> images =savedInstanceState.getStringArrayList(BUNDLE_IMAGES);
}
}
有关保存/恢复片段状态的更多信息:Once for all, how to correctly save instance state of Fragments in back stack?
更新:使用custom图片缓存:
private LruCache<String, Bitmap> mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
...
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}