我会尝试解释我的问题,同时仍然有意义。
我的MainActivity基本上在带有自定义ArrayAdapter的ListView中显示很多帖子,比如Twitter提要。这个ListView包含一堆TextViews和ImageViews,还有一个LinearLayout。我有LinearLayout显示附加的图片如果有任何图片附加到消息。由于我不知道附加了多少张图片,因此我在“私有类StreamAdapter extends ArrayAdapter”类中解决了这个问题:
// Are there pictures attached to the message?
if((dataList.get(position).getAttachments().size()) != 0)
{
ArrayList<ImageView> attachedList = new ArrayList<ImageView>();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT;
for(int i = 0; i < dataList.get(position).getAttachments().size(); i++)
{
String attachmentThumbnailURL= "http://img.photobucket.com/albums/v132/HellSong/cake_2.jpg";
final String attachmentURL= "http://img.photobucket.com/albums/v132/HellSong/cake_2.jpg";
attachedList.add(i, new ImageView(MainActivity.this));
// Make clicking the thumbnail open the actual picture in the browser
attachedList.get(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(attachmentURL));
startActivity(intent);
}
});
attachedList.get(i).setAdjustViewBounds(true);
attachedList.get(i).setId(i);
attachedList.get(i).setTag(attachmentThumbnailURL);
attachedList.get(i).setPadding(0, 5, 5, 5);
attachedList.get(i).setLayoutParams(params);
new ImageDownloadTask().execute(attachedList.get(i));
holder.attachments.addView(attachedList.get(i));
}
holder.attachments.setVisibility(convertView.VISIBLE);
}
dataList是显示的消息的ArrayList。它几乎完美无缺,除了当我滚动浏览整个消息列表(比手机屏幕占用更多)时,当我看到一条带有附加图片的消息时,它会加载图片好像它有两张附图。它没有。然后它不断添加更多,如果我继续前进整个列表视图可能会填充我附加的图片。任何人都知道如何解决这个问题..?
编辑(问题和想法): 我猜测我的图片多次加载的原因是每次帖子在屏幕上可见时都会触发该方法?也许解决方案可能是拥有一个Int-array,每当我在帖子中加载图片时,我会在数组中查找帖子ID。如果它在那里,我不加载图片,如果它不在那里我加载图片并将帖子ID添加到int数组,所以它不会再次加载?
编辑2: 它似乎不起作用,因为现在当我第一次加载图片然后滚动...当我滚动回帖子时,根本没有图片。所以看起来它既不是也不是一大堆。有点烦人。
答案 0 :(得分:0)
我会让arraylist全局化,以便我可以保存它以进行配置更改但是你的问题是由于你每次都需要更新屏幕时创建一个新的arraylist。持有者不断获得它拥有的新副本,并且它们都附加到它已经存在的内容上。您可能希望在想要刷新图像时刷新持有者,您可能想尝试这个
//Flush the holder in case it can be flushed, set contents to null
ArrayList<ImageView> attachedList = new ArrayList<ImageView>();//global variable that I clear every time I want to reload data
// Are there pictures attached to the message?
if((dataList.get(position).getAttachments().size()) != 0)
{
attachedList.clear();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT;
for(int i = 0; i < dataList.get(position).getAttachments().size(); i++)
{
String attachmentThumbnailURL= "http://img.photobucket.com/albums/v132/HellSong/cake_2.jpg";
final String attachmentURL= "http://img.photobucket.com/albums/v132/HellSong/cake_2.jpg";
attachedList.add(i, new ImageView(MainActivity.this));
// Make clicking the thumbnail open the actual picture in the browser
attachedList.get(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(attachmentURL));
startActivity(intent);
}
});
attachedList.get(i).setAdjustViewBounds(true);
attachedList.get(i).setId(i);
attachedList.get(i).setTag(attachmentThumbnailURL);
attachedList.get(i).setPadding(0, 5, 5, 5);
attachedList.get(i).setLayoutParams(params);
new ImageDownloadTask().execute(attachedList.get(i));
holder.attachments.addView(attachedList.get(i));
}
holder.attachments.setVisibility(convertView.VISIBLE);
}
我知道它很晚但可能对其他人有帮助。