ListView复用导致图片错乱及闪烁问题

关于ViewHolder复用,我就不说明了,关键是复用导致出现的问题


网络的好坏,我们请求的图片并不是很稳定,那么我们假设一屏可以显示6条数据,利用复用,我们的第7条数据view就是我们的第1条数据view,随之手势不断的滑动,复用的问题,就出现,特别是上网请求网络图片, 我们在显示第7条数据时,图片还是第1个数据图片,因为第7条很可以没有加载完成,导致view复用,图片错乱,那么如何解决呢

  1. 给每个 ImageView设置tag ,tag 的内容就是唯一的url
  2. 异步加载类中输入url
  3. 在异步加载类,完成后,对比ImageView的tag与url是否一致
  4. 一致就设置图片,不一致,就不设置,同时setImageBitmap(null)来解决闪烁问题
public class Myadapter extends BaseAdapter {

    private List list;

    public Myadapter(List list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Info.DataBean bean = list.get(position);
        MyViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
            viewHolder = new MyViewHolder();
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            viewHolder.title = (TextView) convertView.findViewById(R.id.item_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MyViewHolder) convertView.getTag();
        }

        viewHolder.title.setText(bean.getTitle());
        viewHolder.iv.setTag(bean.getImgs().get(0)); //内容为url的tag
        viewHolder.iv.setImageBitmap(null);  //解决闪烁
        new MyNet(viewHolder.iv).execute(bean.getImgs().get(0)); //加载类中url为图片的url

        return convertView;
    }

    static class MyViewHolder {
        private ImageView iv;
        private TextView title;
        private CheckBox cb;
    }

    class MyNet extends AsyncTaskbyte[]> {

        private ImageView iv;
        private String path;
        public MyNet(ImageView imageView) {
            this.iv = imageView;
        }
        @Override
        protected byte[] doInBackground(String... params) {
            try {
                URL url = new URL(params[0]);
                path = params[0];
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                con.setConnectTimeout(5000);
                con.setReadTimeout(5000);
                con.connect();
                if (con.getResponseCode() == HttpURLConnection.HTTP_OK && !isCancelled()) {
                    InputStream inputStream = con.getInputStream();
                    int len;
                    byte[] buff = new byte[1024 * 4];
                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
                    while ((len = inputStream.read(buff)) != -1) {
                        bao.write(buff, 0, len);
                    }
                    inputStream.close();
                    return bao.toByteArray();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        };

        @Override
        protected void onPostExecute(byte[] bytes) {
            super.onPostExecute(bytes);
            if (bytes != null && iv.getTag().equals(path)) { //加载类中,回来直接判断是不是最新的tag 也 就是最新滑动的item
                iv.setImageBitmap(BitmapFactory.decodeByteArray(bytes,0,bytes.length));
            }
        }
    }
}

你可能感兴趣的:(android总结)