我正在使用通用图像加载器从互联网下载图像。有些图片被设置为头像,所以我将它们四舍五入,其他图片被拉伸。问题是列表视图在滚动时显示错误的图像,一段时间后显示正确的图像。我搜索了stackoverflow的类似问题,但我无法正常工作。
我正在使用自定义适配器和getview代码以及用于舍入和拉伸图像的代码如下:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if(view == null)
{
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.whatsnew_listitem, null);
holder = new ViewHolder();
holder.playButton = (ImageButton) view.findViewById(R.id.btn_playVideo);
holder.mediaThumbnail = (ImageView) view.findViewById(R.id.imageview_mediaImage);
holder.avatar = (ImageView) view.findViewById(R.id.imageview_avatar);
holder.artistName = (TextView) view.findViewById(R.id.textview_artistName);
holder.timestamp = (TextView) view.findViewById(R.id.textview_timestamp);
holder.status = (TextView) view.findViewById(R.id.textview_title);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
pos = position;
Item item = items.get(position);
if(item.getArtistName() != "")
{
// set thumbnail
String thumbnailURL = "";
holder.mediaThumbnail.setVisibility(View.INVISIBLE);
if(item.getUpdateType() == Config.UPDATE_TYPE_1)
{
holder.playButton.setVisibility(View.GONE);
thumbnailURL = item.getPictureUri();
if(thumbnailURL != null)
{
ImageLoader.getInstance().loadImage(thumbnailURL, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
setImage(loadedImage, imageView, Config.MEDIA);
mediaImages[position] = true;
imageView.setVisibility(View.VISIBLE);
}
});
}
holder.status.setText(item.getMessage());
}
else if(item.getUpdateType() == Config.UPDATE_TYPE_2)
{
//play button
holder.playButton.setVisibility(View.VISIBLE);
//set media image
thumbnailURL = item.getVideoThumbnailUri();
if(thumbnailURL != null)
{
ImageLoader.getInstance().loadImage(thumbnailURL, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
setImage(loadedImage, imageView, Config.MEDIA);
mediaImages[position] = true;
imageView.setVisibility(View.VISIBLE);
}
});
}
//set youtube status
holder.status.setText(item.getTitle());
holder.playButton.bringToFront();
holder.playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(item.getVideoId()!= null) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Config.YOUTUBE_URI + item.getVideoId() ));
context.startActivity(intent);
}
}
});
}
//set avatar
String avatarURL = item.getAvatarImageUri();
holder.avatar.setVisibility(View.INVISIBLE);
if(avatarURL != null)// && avatarImages[position] == false)
{
ImageLoader.getInstance().loadImage(avatarURL, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
setImage(loadedImage, imageView, Config.AVATAR);
avatarImages[position] = true;
imageView.setVisibility(View.VISIBLE);
}
});
}
//set artist name
holder.artistName.setText(item.getArtistName());
//set timestamp
if(timerList[position] == false)
{
updateTime(holder.timestamp, item);
}
}
view.setTag(holder);
return view;
}
//@Override
public void setImage(final Bitmap bitmap, final ImageView imageView, final String imagetype) {
// TODO Auto-generated method stub
Activity activity = (Activity) context;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Bitmap croppedBmp = null;
int imageHeight = bitmap.getHeight();
int imageWidth = bitmap.getWidth();
if(imagetype == Config.MEDIA)
{
if(imageHeight <imageWidth) {
croppedBmp = Bitmap.createScaledBitmap( bitmap, dp, dp, true);
imageView.setImageBitmap(croppedBmp);
}
}
else if(imagetype == Config.AVATAR)
{
int radius;
if(imageWidth > imageHeight)
{
croppedBmp = Bitmap.createBitmap(imageHeight, imageHeight, Bitmap.Config.ARGB_4444);
radius = imageHeight/2;
}
else
{
croppedBmp = Bitmap.createBitmap(imageWidth, imageWidth, Bitmap.Config.ARGB_4444);
radius = imageWidth/2;
}
Canvas canvas = new Canvas(croppedBmp);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, imageWidth, imageHeight);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(radius, radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
imageView.setImageBitmap(croppedBmp);
ImageLoader.getInstance().displayImage(uri, imageView, listener)
notifyDataSetChanged();
}
}
});
}
答案 0 :(得分:1)
试试这个例子 http://android.amberfog.com/?p=296
使用此方法
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
并在getView()
中获取如下类型int type = getItemViewType(position);
我按照示例
解决了我的问题答案 1 :(得分:0)
在if else条件之外初始化视图
if(view == null)
{
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.whatsnew_listitem, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.playButton = (ImageButton) view.findViewById(R.id.btn_playVideo);
holder.mediaThumbnail = (ImageView) view.findViewById(R.id.imageview_mediaImage);
holder.avatar = (ImageView) view.findViewById(R.id.imageview_avatar);
holder.artistName = (TextView) view.findViewById(R.id.textview_artistName);
holder.timestamp = (TextView) view.findViewById(R.id.textview_timestamp);
holder.status = (TextView) view.findViewById(R.id.textview_title);
答案 2 :(得分:0)
使用ImageLoader的覆盖方法
@Override
public void onLoadingStarted(String imageUri, View view) {
ImageLoader.getInstance().displayImage(imageUri, (ImageView) view);
}
并在显示选项中使用将显示的存根图像,直到从URL加载图像。
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.com_facebook_profile_default_icon).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).defaultDisplayImageOptions(defaultOptions).build();
答案 3 :(得分:0)
每一刻ListView都会显示几个元素(那些“适合”显示的元素),而不是创建一个新元素,在轻弹过程中,新元素替换列表中的旧元素
试试这个:
public class Info {
private ImageView mediaThumbnail;
private ImageView avatar;
...
public ImageView getMediaThumbnail() {
return mediaThumbnail;
}
public void setMediaThumbnail(ImageView mediaThumbnail) {
this.mediaThumbnail = mediaThumbnail;
}
...
}
在适配器中添加到ViewHolder字段Info info
并编写下一个代码
public View getView(int position, View convertView, ViewGroup parent) {
row = convertView;
Info info = getItem(position);
if (null == row)
setupRow(parent, info);
else
getRow(info);
...
}
private void setupRow(ViewGroup parent, Info info) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.playButton = (ImageButton) view.findViewById(R.id.btn_playVideo);
holder.mediaThumbnail = (ImageView) view.findViewById(R.id.imageview_mediaImage);
holder.avatar = (ImageView) view.findViewById(R.id.imageview_avatar);
holder.artistName = (TextView) view.findViewById(R.id.textview_artistName);
holder.timestamp = (TextView) view.findViewById(R.id.textview_timestamp);
holder.status = (TextView) view.findViewById(R.id.textview_title);
view.setTag(holder);
}
private void getRow(DownloadInfo info) {
holder = (ViewHolder) row.getTag();
holder.info.setMediaThumbnail(null);
...
holder.info = info;
holder.info.setMediaThumbnail(holder.mediaThumbnail);
...
}