繁体   English   中英

如何在Android的ListView中设置下载的图像?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM