我正在为Android创建一个音乐播放器应用程序。在我的自定义列表视图中,我有标题,艺术家和专辑封面。我使用通用图像加载器在imageview中加载专辑封面。问题是listview在滚动时滞后。在日志中,每当我向上或向下滚动时,我都会检查listviewadapter是否一次又一次地运行。有没有办法阻止listviewadapter一次又一次地更新。这是我的自定义listviewadapter。
import android.content.ContentResolver;
import android.content.ContentUris;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.io.InputStream;
import java.util.List;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
public class ListViewAdapter extends ArrayAdapter<WorldPopulation> {
// Declare Variables
Context context;
LayoutInflater inflater;
List<WorldPopulation> worldpopulationlist;
private SparseBooleanArray mSelectedItemsIds;
public ListViewAdapter(Context context, int resourceId,
List<WorldPopulation> worldpopulationlist) {
super(context, resourceId, worldpopulationlist);
mSelectedItemsIds = new SparseBooleanArray();
this.context = context;
this.worldpopulationlist = worldpopulationlist;
inflater = LayoutInflater.from(context);
}
private class ViewHolder {
TextView title;
TextView artist;
ImageView albumArt;
}
public View getView(int position, View view, ViewGroup parent) {
setupImageLoader();
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.songs_artist_album_art, null);
// Locate the TextViews in listview_item.xml
holder.title = (TextView) view.findViewById(R.id.songs_title);
holder.artist = (TextView) view.findViewById(R.id.songs_artist);
// Locate the ImageView in listview_item.xml
holder.albumArt = (ImageView) view.findViewById(R.id.imageView6);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews
holder.title.setText(worldpopulationlist.get(position).gettitle());
holder.artist.setText(worldpopulationlist.get(position).getartist());
// Capture position and set to the ImageView
ImageLoader imageLoader = ImageLoader.getInstance();
//noinspection deprecation
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.album_art)
.showImageOnFail(R.drawable.album_art)
.showImageOnLoading(R.drawable.album_art).build();
// Bitmap artwork = null;
// try {
// Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
// Uri uri = ContentUris.withAppendedId(sArtworkUri, Long.valueOf(worldpopulationlist.get(position).getalbumArt()));
// ContentResolver res = context.getContentResolver();
// InputStream in = res.openInputStream(uri);
// artwork = BitmapFactory.decodeStream(in);
//
// } catch (Exception e) {
// Log.e("Exception", e.toString());
// artwork = BitmapFactory.decodeResource(context.getResources(),R.drawable.album_art);
// }
// holder.albumArt.setImageBitmap(artwork);
imageLoader.displayImage("content://media/external/audio/albumart/" + worldpopulationlist.get(position).getalbumArt(),holder.albumArt,options);
return view;
}
@Override
public void remove(WorldPopulation object) {
worldpopulationlist.remove(object);
notifyDataSetChanged();
}
public List<WorldPopulation> getWorldPopulation() {
return worldpopulationlist;
}
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}
public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}
public int getSelectedCount() {
return mSelectedItemsIds.size();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}
private void setupImageLoader(){
// UNIVERSAL IMAGE LOADER SETUP
//noinspection deprecation
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
//noinspection deprecation
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache()).discCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
// END - UNIVERSAL IMAGE LOADER SETUP
}
}
由于