毕加索在 ListFragment 中有很多大位图
Posted
技术标签:
【中文标题】毕加索在 ListFragment 中有很多大位图【英文标题】:Picasso with a lot of big bitmaps in ListFragment 【发布时间】:2015-10-04 13:44:11 【问题描述】:我有一个关于毕加索图书馆的问题。我在 ListFragment 中有 ListView 从服务器下载图像。我正在使用 BaseAdapter 填充该 ListView。问题是当我遇到大量图像(10+)时,当毕加索尝试加载它们时,我遇到了加载问题。基本上我必须在它们加载之前稍等片刻,突然它们都同时加载了。仅当我使用此片段转到另一个活动并返回 MainActivity 时才会发生这种情况这是我的 baseAdapter 源代码:
public class MySimpleArrayAdapter extends BaseAdapter
private final Context context;
private final ArrayList<String> values;
private final double[] lat2;
private final double[] lon2;
private final double lat;
private final double lon;
private final int[] id;
private final int[] timestamp;
private final int[] likes;
private final String[] tags;
private final String[] statusText;
private final String[] imgLink;
private final String[] author;
public MySimpleArrayAdapter(Context context, ArrayList<String> values, double[] lat2,double[] lon2,double lat, double lon, int[] id,int[] timestamp,int[] likes, String[] tags,String[] statusText,String[] imgLink, String[] author)
this.context = context;
this.values = values;
this.lat2 = lat2;
this.lon2 = lon2;
this.lat = lat;
this.lon = lon;
this.id = id;
this.timestamp = timestamp;
this.likes = likes;
this.tags = tags;
this.statusText = statusText;
this.imgLink = imgLink;
this.author = author;
@Override
public int getCount()
if(lat2!=null)
return lat2.length;
else
return 0;
@Override
public Object getItem(int position)
return null;
@Override
public long getItemId(int position)
return position;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
View v = convertView;
if (convertView == null)
LayoutInflater li = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.simple_list_item, parent, false);
try
int distance = 0;
if (lat2 != null && lon2 != null)
distance = (int) computeDistanceBetween(new LatLng(lat, lon), new LatLng(lat2[getCount()-position-1], lon2[getCount()-position-1]));
ImageButton like = (ImageButton)v.findViewById(R.id.likeButton);
like.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
likeStatus(id[getCount()-position-1],author[getCount()-position-1]);
);
TextView firsteLine = (TextView) v.findViewById(R.id.StatusText);
ImageView imageView = (ImageView) v.findViewById(R.id.StatusImage);
imageView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String author1 = null;
String link = null;
String text3 = null;
String tags1 = null;
int distance = 0;
int ts = 0;
int id1 = 0;
int likes1 = 0;
LatLng ll = new LatLng(lat, lon);
id1 = id[getCount()-position-1];
likes1 = likes[getCount()-position-1];
author1 = author[getCount()-position-1];
link = imgLink[getCount()-position-1];
text3 = statusText[getCount()-position-1];
if (lat2 != null&&lon2 != null)
distance = (int) computeDistanceBetween(ll, new LatLng(lat2[getCount()-position-1], lon2[getCount()-position-1]));
else
distance=0;
ts = timestamp[getCount()-position-1];
tags1 = tags[getCount()-position-1];
Intent intent = new Intent(context, ShowMarkerContent.class);
intent.putExtra("id", id1);
intent.putExtra("likes", likes1);
intent.putExtra("tags", tags1);
intent.putExtra("link", link);
intent.putExtra("author", author1);
intent.putExtra("text", text3);
intent.putExtra("distance", distance);
intent.putExtra("time", ts);
intent.putExtra("number", getCount()-position-1);
context.startActivity(intent);
);
Picasso.with(context).cancelRequest(imageView);
Picasso.with(context).load("http://files.guri.sk/" + imgLink[getCount()-position-1]).fit().centerCrop().error(R.mipmap.ic_launcher).placeholder(R.drawable.progress_animation).into(imageView);
Log.d("Tag","http://files.guri.sk/"+imgLink[getCount()-position-1]);
if (distance > 1000)
distance = distance / 1000;
firsteLine.setText(values.get(getCount()-position-1) + " (" + MySearchAdapter.caluculateTimeAgo(timestamp[getCount()-position-1]) + ", " + String.valueOf(distance) + "km) ");
else
firsteLine.setText(values.get(getCount()-position-1) + " (" + MySearchAdapter.caluculateTimeAgo(timestamp[getCount()-position-1]) + ", " + String.valueOf(distance) + "m) ");
catch (IndexOutOfBoundsException e)
return v;
public void likeStatus(int id, String user)
try
JSONObject toSend = new JSONObject();
toSend.put("user", user);
toSend.put("id", id);
JSONTransmitterLike transmitter = new JSONTransmitterLike();
String resp = null;
try
resp = transmitter.execute(new JSONObject[]toSend).get();
resp = resp.replace("\n", "").replace("\r", "");
Log.d("Tag", resp);
catch (InterruptedException e)
e.printStackTrace();
catch (ExecutionException e)
e.printStackTrace();
JSONObject jObj = new JSONObject(resp);
int numberOfLikes = jObj.getInt("like_total");
int result = jObj.getInt("result");
int liked = jObj.getInt("liked");
if (result == 2)
unLike(id, user);
else
Toast.makeText(context,"You like this",Toast.LENGTH_SHORT).show();
catch (JSONException e)
e.printStackTrace();
public void unLike(int id, String user)
try
JSONObject toSend = new JSONObject();
toSend.put("user", user);
toSend.put("id", id);
JSONTransmitterUnlike transmitter = new JSONTransmitterUnlike();
String resp = null;
try
resp = transmitter.execute(new JSONObject[]toSend).get();
resp = resp.replace("\n", "").replace("\r", "");
Log.d("Tag", resp);
catch (InterruptedException e)
e.printStackTrace();
catch (ExecutionException e)
e.printStackTrace();
JSONObject jObj = new JSONObject(resp);
int numberOfLikes = jObj.getInt("like_total");
int result = jObj.getInt("result");
int liked = jObj.getInt("liked");
Toast.makeText(context,"You don't like this",Toast.LENGTH_SHORT).show();
catch (JSONException e)
e.printStackTrace();
【问题讨论】:
【参考方案1】:只有当这是一个回收视图时,您才应该取消请求,而不是针对每个视图。
干杯!
【讨论】:
您好,谢谢您的回复。嗯,我用这条线的时候和不用的时候效果是一样的 你把“Picasso.with(context).cancelRequest(imageView);”放了吗进入其他? (当 convertView 不为空时)? 我的意思是不管我用不用这条线,结果都是一样的。以上是关于毕加索在 ListFragment 中有很多大位图的主要内容,如果未能解决你的问题,请参考以下文章
Kotlin 中如何使用 Picasso 将 URL 转换为位图?