[英]how to set the downloaded images in listview in android?
我從服務器下載了圖像並將其添加到ArrayList
,並在ListView
使用了它們
當我在ListView
滾動時,圖像在刷新,出現問題的位置在哪里。
誰能幫我弄清楚為什么滾動時圖像沒有刷新?
這是我的代碼:
public class CustomAdapter extends BaseAdapter {
ArrayList<String> p_id;
ArrayList<String> p_name;
Context context;
ArrayList<String> imageId;
ArrayList<String> wash_v;
ArrayList<String> dry_v;
ArrayList<String> iron_v;
Holder holder;
Intent i;
private static LayoutInflater inflater = null;
@SuppressWarnings("static-access")
public CustomAdapter(Context con, ArrayList<String> pid,
ArrayList<String> pname, ArrayList<String> pimg,
ArrayList<String> wash, ArrayList<String> dry,
ArrayList<String> iron) {
p_id = pid;
context = con;
p_name = pname;
imageId = pimg;
wash_v = wash;
dry_v = dry;
iron_v = iron;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return imageId.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv1, tv2, tv3;
ImageView img;
LinearLayout ll1, ll2, ll3, llRow;
}
@SuppressLint("UseValueOf")
@Override
public View getView(final int position, final View convertView,
ViewGroup parent) {
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = inflater.inflate(R.layout.dynamic, null);
Holder holder = new Holder();
holder.ll1 = (LinearLayout) v.findViewById(R.id.ll1);
holder.ll2 = (LinearLayout) v.findViewById(R.id.ll2);
holder.ll3 = (LinearLayout) v.findViewById(R.id.ll3);
holder.llRow = (LinearLayout) v.findViewById(R.id.llRow);
holder.tv1 = (TextView) v.findViewById(R.id.tv1);
holder.tv2 = (TextView) v.findViewById(R.id.tv3);
holder.tv3 = (TextView) v.findViewById(R.id.tv5);
holder.img = (ImageView) v.findViewById(R.id.ivImage);
final TextView tvVal = (TextView) v.findViewById(R.id.tvVal);
final TextView tvVal1 = (TextView) v.findViewById(R.id.tvVal1);
final TextView tvVal2 = (TextView) v.findViewById(R.id.tvVal2);
v.setTag(holder);
}
Holder viewHolder = (Holder) v.getTag();
viewHolder.tv1.setText(wash_v.get(position));
viewHolder.tv2.setText(iron_v.get(position));
viewHolder.tv3.setText(dry_v.get(position));
Picasso.with(context).load(imageId.get(position)).into(viewHolder.img);
}
return v;
}
}
關鍵是,當向下滾動
ListView
時,我不想刷新圖像。
問題是您沒有正確使用支架圖案。
每次滾動時,都會調用getView,這是您的錯誤,您每次都在創建一個新的holder,一次又一次地擴大視圖,您沒有在重用行,而是在重新創建它們。
我將向您展示一部分希望對您有所幫助的代碼。
public RecentsAdapter(Context context, List<ParseObject> conversas) {
super(context, R.layout.conversa_row, conversas);
this.context = context;
this.conversas = conversas;
}
static class ViewHolder{
public TextView chatName;
public TextView lastMessage;
public ImageView chatImage;
public TextView lastDate;
public TextView counter;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v= convertView;
if (v == null) {
v = LayoutInflater.from(context).inflate(R.layout.conversa_row, parent,false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.chatName=(TextView) v.findViewById(R.id.chatName);
viewHolder.chatImage=(ImageView) v.findViewById(R.id.chatImage);
viewHolder.lastMessage=(TextView) v.findViewById(R.id.lastMessage);
viewHolder.lastDate=(TextView) v.findViewById(R.id.lastDate);
viewHolder.counter = (TextView) v.findViewById(R.id.counter);
v.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) v.getTag();
ParseObject conversa = conversas.get(position);
holder.lastMessage.setText("example");
holder.lastDate.setText("example");
return v;
}
如果在適配器中使用此功能,則會看到不同之處。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.