SoFunction
Updated on 2025-04-10

Solve the problem of pictures flashing when Android swipes quickly

The problem of quickly sliding the image and flashing, and the processing of image loading and other things will not be introduced here. The main purpose is to maintain a LinkedHashMap in Adapter to solve the above problems.

package ;
 
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
import ;
import ;
 
import ;
import ;
import ;
import ;
 
import ;
import ;
import ;
import ;
 
/**
 * Created by wangcong on 15-1-8.
 */
public class OrderFinishAdapter extends BaseAdapter {
 
  //convertview id
  private final static int BASE_ID = 0x0fff00;
 
  private Context mContext;
  private List<Map<String, Object>> mAllList;
 
  // Picture loading related  CCImageLoader mImageLoader;
  // Used to cache images to reduce the phenomenon of falling frames when swiping quickly  final LinkedHashMap<String, SoftReference<Bitmap>> linkedHashMap;
  final int MAX_SIZE = 16;
 
  public OrderFinishAdapter(Context context, List<Map<String, Object>> list) {
     = context;
     = list;
    linkedHashMap = new LinkedHashMap<String, SoftReference<Bitmap>>(16, 0.75f, true) {
 
      private static final long serialVersionUID = 1L;
 
      @Override
      protected boolean removeEldestEntry(Entry<String, SoftReference<Bitmap>> eldest) {
        boolean flag = size() > MAX_SIZE;
        if (flag) {
          SoftReference<Bitmap> softReference = ();
          Bitmap bitmap = ();
          if (bitmap != null) ();
          remove(());
        }
        return flag;
      }
    };
  }
 
  public int getCount() {
    return ();
  }
 
  public Object getItem(int position) {
    return (position);
  }
 
  public long getItemId(int position) {
    return position;
  }
 
  @SuppressWarnings("deprecation")
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
      convertView = (mContext).inflate(.adapter_finish_item, parent, false);
      holder = new ViewHolder();
      (holder, convertView);
      (holder);
    } else {
      holder = (ViewHolder) ();
    }
    (BASE_ID + position);
    final Map<String, Object> map = (position);
    //Processing pictures    SoftReference<Bitmap> softReference = (("orderImgUrl"));
    Bitmap bitmap = softReference == null ? null : ();
    if (bitmap == null) {
      (.default_image_error);
      if (mImageLoader == null)
        mImageLoader = new ().needCacheInDisk().outSize(120, 120)
            .callback(new CCImageLoaderCallback() {
              @Override
              public void onSuccess(Bitmap bitmap, Object... objs) {
                (bitmap, objs);
                if (bitmap != null) { //The image is successfully loaded and processed                  ImageView imageView = (ImageView) objs[0];
                  (new BitmapDrawable((), bitmap));
                  SoftReference<Bitmap> soft = new SoftReference<Bitmap>(bitmap);
                  (objs[1] + "", soft);
                  bitmap = null;
                }
              }
            }).build();
      (("orderImgUrl") + "", , ("orderImgUrl"));
    } else {
      (new BitmapDrawable((), bitmap));
    }
 
    
    return convertView;
  }
 
  static class ViewHolder {
 
    @CCInjectRes(.order_item_image)
    ImageView image;
  }
}

The above is the entire content of this article, I hope you like it.