图片加载库Glide——解决图片错乱+无法设置tag

今天在写一个图片加载类ImageLoader,在使用的时候想用Glide替代我写的ImageLoader,然后问题就出来了!!!

第一个问题:在使用自己写的ImageLoader的时候,为了防止item复用导致的图片错乱,设置了Tag 如下代码

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.image_list_item,parent, false);
                holder = new ViewHolder();
                holder.imageView = (ImageView) convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ImageView imageView = holder.imageView;
            final String tag = (String)imageView.getTag();
            final String uri = getItem(position);
            if (!uri.equals(tag)) {
                imageView.setImageDrawable(mDefaultBitmapDrawable);
            }
            if (mIsGridViewIdle && mCanGetBitmapFromNetWork) {
                imageView.setTag(uri);
                mImageLoader.bindBitmap(uri, imageView, mImageWidth, mImageWidth);
            }
            return convertView;
        }

使用setTag(object),这里使用了每个图片的url地址来作为Tag来防止图片错乱
当使用Glide来替换ImageLoader,代码如下:

          public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_gridview, parent, false);
                holder = new ViewHolder();
                holder.imageView = (ImageView) convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ImageView imageView = holder.imageView;
            final String tag = (String) imageView.getTag();
            final String uri = getItem(position);
            if (!uri.equals(tag)) {
                imageView.setImageDrawable(mDefaultBitmapDrawable);
            }

            if (mIsGridViewIdle && mCanGetBitmapFromNetWork) {
                imageView.setTag(uri);
                Glide.with(getApplicationContext()).load(uri).placeholder(R.drawable.image_default).crossFade().into(imageView);
                //mImageLoader.bindBitmap(uri, imageView, mImageWidth, mImageWidth);
            }
            return convertView;
        }

运行结果报错:java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting
然后将imageView设置Tag的代码删除,程序正常运行 。
但这个时候哟与出现一个问题,那就是图片错乱了。

第二个问题:删除ImageView的Tag的时候 图片错乱

解决问题的方法:使用setTag(int,object)方法设置tag 这样就解决了上面两个问题

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_gridview, parent, false);
                holder = new ViewHolder();
                holder.imageView = (ImageView) convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ImageView imageView = holder.imageView;
            final String tag = (String) imageView.getTag(R.id.imageloader_uri);
            final String uri = getItem(position);
            if (!uri.equals(tag)) {
                imageView.setImageDrawable(mDefaultBitmapDrawable);
            }

            if (mIsGridViewIdle && mCanGetBitmapFromNetWork) {
                imageView.setTag(R.id.imageloader_uri,uri);
                Glide.with(getApplicationContext()).load(uri).placeholder(R.drawable.image_default).crossFade().into(imageView);
                //mImageLoader.bindBitmap(uri, imageView, mImageWidth, mImageWidth);
            }
            return convertView;
        }

这里的R.id.imageloader_uri是values下新建的ids.xml:


<resources>

    <item name="imageloader_uri" type="id"/>

resources>

你可能感兴趣的:(Android学习,Android自主学习笔记)