Universal Image Loader - listView 被清除
Universal Image Loader - listView was cleared
几个小时前,我询问了关于 AUIL 调用太多 getView() 的问题。 (Universal Image Loader - call too many getView())
通过解答,我解决了getView()调用次数过多的问题。
但是,我仍然有问题。
当 activity 启动时,我可以看到列表。但是,不久之后,列表视图被删除(或清除)。列表视图中没有图像出现或加载。
这是我的源代码。
public class MainActivity extends Activity{
AsyncTask<Void, Void, Void> asyncTask, registerTask;
DisplayImageOptions options;
ArrayList<HashMap<String, Object>> feedList = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int cacheSize = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass() * 1024 * 1024 / 8;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(metrics.widthPixels, metrics.heightPixels)
.diskCacheExtraOptions(metrics.widthPixels, metrics.heightPixels, null)
.memoryCache(new LruMemoryCache(cacheSize))
.memoryCacheSize(cacheSize)
.memoryCacheSizePercentage(13)
.diskCache(new UnlimitedDiscCache(StorageUtils.getCacheDirectory(this)))
.diskCacheSize(100 * 1024 * 1024)
.diskCacheFileCount(200)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new BaseImageDownloader(this))
.imageDecoder(new BaseImageDecoder(false))
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.build();
ImageLoader.getInstance().init(config);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
asyncTask = new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
StyleFeedServerUtils serverUtils = new StyleFeedServerUtils();
feedList = serverUtils.getStyleFeedList(MainActivity.this);
return null;
}
@Override
protected void onPostExecute(Void result) {
if(feedList != null && feedList.size() > 0){
final ListView listView = (ListView)findViewById(R.id.list);
listView.setAdapter(new ImageAdapter());
PauseOnScrollListener listener = new PauseOnScrollListener(ImageLoader.getInstance(), true, true);
listView.setOnScrollListener(listener);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("fashion", "onItemClick, position : "+position);
}
});
}
asyncTask.cancel(!isCancelled());
asyncTask = null;
}
};
asyncTask.execute(null, null, null);
}
@Override
public void onDestroy() {
super.onDestroy();
if (registerTask != null) {
registerTask.cancel(true);
}
//GCMRegistrar.onDestroy(this);
AnimateFirstDisplayListener.displayedImages.clear();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class ImageAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
ImageAdapter() {
inflater = LayoutInflater.from(MainActivity.this);
}
@Override
public int getCount() {
return feedList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView == null){
view = inflater.inflate(R.layout.style_feed_list_view, parent, false);
view.setTag((ImageView)view.findViewById(R.id.style_feed_image));
}
ImageLoader.getInstance().displayImage(feedList.get(position).get("URL").toString(), (ImageView)view.getTag(), options, animateFirstListener);
return view;
}
}
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
}
请帮忙。
有时由于 RoundedBitmapDisplayer 只是评论 .displayer(new RoundedBitmapDisplayer(20)) 这一行并再次尝试 运行 而造成问题。
如果您想使用圆形图像视图,请尝试下面的库
https://github.com/vinc3m1/RoundedImageView
几个小时前,我询问了关于 AUIL 调用太多 getView() 的问题。 (Universal Image Loader - call too many getView())
通过解答,我解决了getView()调用次数过多的问题。 但是,我仍然有问题。 当 activity 启动时,我可以看到列表。但是,不久之后,列表视图被删除(或清除)。列表视图中没有图像出现或加载。
这是我的源代码。
public class MainActivity extends Activity{
AsyncTask<Void, Void, Void> asyncTask, registerTask;
DisplayImageOptions options;
ArrayList<HashMap<String, Object>> feedList = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int cacheSize = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass() * 1024 * 1024 / 8;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(metrics.widthPixels, metrics.heightPixels)
.diskCacheExtraOptions(metrics.widthPixels, metrics.heightPixels, null)
.memoryCache(new LruMemoryCache(cacheSize))
.memoryCacheSize(cacheSize)
.memoryCacheSizePercentage(13)
.diskCache(new UnlimitedDiscCache(StorageUtils.getCacheDirectory(this)))
.diskCacheSize(100 * 1024 * 1024)
.diskCacheFileCount(200)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new BaseImageDownloader(this))
.imageDecoder(new BaseImageDecoder(false))
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.build();
ImageLoader.getInstance().init(config);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
asyncTask = new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
StyleFeedServerUtils serverUtils = new StyleFeedServerUtils();
feedList = serverUtils.getStyleFeedList(MainActivity.this);
return null;
}
@Override
protected void onPostExecute(Void result) {
if(feedList != null && feedList.size() > 0){
final ListView listView = (ListView)findViewById(R.id.list);
listView.setAdapter(new ImageAdapter());
PauseOnScrollListener listener = new PauseOnScrollListener(ImageLoader.getInstance(), true, true);
listView.setOnScrollListener(listener);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("fashion", "onItemClick, position : "+position);
}
});
}
asyncTask.cancel(!isCancelled());
asyncTask = null;
}
};
asyncTask.execute(null, null, null);
}
@Override
public void onDestroy() {
super.onDestroy();
if (registerTask != null) {
registerTask.cancel(true);
}
//GCMRegistrar.onDestroy(this);
AnimateFirstDisplayListener.displayedImages.clear();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class ImageAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
ImageAdapter() {
inflater = LayoutInflater.from(MainActivity.this);
}
@Override
public int getCount() {
return feedList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView == null){
view = inflater.inflate(R.layout.style_feed_list_view, parent, false);
view.setTag((ImageView)view.findViewById(R.id.style_feed_image));
}
ImageLoader.getInstance().displayImage(feedList.get(position).get("URL").toString(), (ImageView)view.getTag(), options, animateFirstListener);
return view;
}
}
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
}
请帮忙。
有时由于 RoundedBitmapDisplayer 只是评论 .displayer(new RoundedBitmapDisplayer(20)) 这一行并再次尝试 运行 而造成问题。 如果您想使用圆形图像视图,请尝试下面的库 https://github.com/vinc3m1/RoundedImageView