title: How to keep the already Loaded Images in the Gridview after Scrolling in Android
body: <p>I have a <code>GridView</code> that shows images from a sdcard. I use <code>AsyncTask</code> in the <code>getview()</code> method (getview loads the bitmap in AsyncTask) from <a href="http://android-er.blogspot.com/2014/09/getview-to-load-images-in-asynctask.html" rel="nofollow">this tutorial</a>.</p>
  
  <p><code>GridView</code> scrolling is fast and loads the images in <code>Gridview</code> fast and positions it correctly but my problem is:</p>
  
  <p>When I scroll the GridView , <code>items that are not visible on the screen can not be held</code>.</p>
  
  <p>And if returned to previous position, the items load again with <code>getview()</code> method.</p>
  
  <p>With the above tutorial when I scroll <code>GridView</code> all the images positions have changed.</p>
  
  <p>I changed <code>getview()</code> (<code>if_else</code> that shows with comment in my code)  and resolved it, but it does not save the previous image and when fast scrolling <code>GridView</code>, at first the default image (<code>ic_launcher</code>) is displayed and after a while, the right image is loaded.
  How to resolve this problem? (hold all images in <code>GridView</code> without reducing scroll speed)</p>
  
  <pre><code>    @Override
      public View getView( final int position, View view, ViewGroup parent) {
  
          final ViewHolder holder;
          //if (view == null) {
              holder = new ViewHolder();
  
              view = inflater.inflate(R.layout.showitems,parent, false);
  
              holder.myimage=(ImageView) view.findViewById(R.id.imagess);
              holder.myimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
              holder.myimage.setPadding(8, 8, 8, 8);
  
  
              view.setTag(holder);
      //      } 
  
      /*  else {
  
              holder = (ViewHolder) view.getTag();
              }*/
  
          new AsyncTask&lt;ViewHolder, Void, Bitmap&gt;() {
  
  
                      private ViewHolder v;
  
                      @Override
                      protected Bitmap doInBackground(ViewHolder... params) {
                          v = params[0];
  
                          Bitmap bm = decodeSampledBitmapFromUri(mylist.get(position).getmypath(), 220, 220);
                          return bm;
  
                      }
  
                      @Override
                      protected void onPostExecute(Bitmap result) {
                          super.onPostExecute(result);
  
                          v.myimage.setImageBitmap(result);
      }
  
                  }
  
                  .execute(holder);
  
  
              view.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
  
                      String this_id= mylist.get(position).getmyid();
                      String this_path=mylist.get(position).getmypath();
  
                      Intent ss=new Intent(mContext,show.class);
                      ss.putExtra("id", this_id);
                      mContext.startActivity(ss);
  
                  }
              });
  
          return view;
  
  
  }
  
  
      public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,int reqHeight) {
  
          Bitmap bm = null;
  
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(path, options);
  
          // Calculate inSampleSize
  
          options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
  
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          bm = BitmapFactory.decodeFile(path, options);
  
  
  
          return bm;
  
      }
  
  
  
  
      public Bitmap decodeSampledBitmapFromUri_VID(String path, int reqWidth,int reqHeight) {
  
          Bitmap bm = null;
  
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
  
          // Calculate inSampleSize
  
          options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
  
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          bm = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
  
  
  
          return bm;
  
      }
  
      public int calculateInSampleSize(
  
              BitmapFactory.Options options, int reqWidth, int reqHeight) {
          // Raw height and width of image
          final int height = options.outHeight;
          final int width = options.outWidth;
          int inSampleSize = 1;
  
          if (height &gt; reqHeight || width &gt; reqWidth) {
              if (width &gt; height) {
                  inSampleSize = Math.round((float) height/ (float) reqHeight);
  
  
              } 
  
              else {
  
                  inSampleSize = Math.round((float) width / (float) reqWidth);
  
              }
  
          }
  
  
          return inSampleSize;
  
      }
  </code></pre>