2015-07-02 7 views
5

Kaydırarak bir önceki içeriği görüntülerkenBir GridLinearLayout ile bir RecyclerView ve özel bir adaptör var. Her öğenin içeriği, bir json kullanılarak indirilen ve ayrıştırılan bir resimdir.Recycler'ı Önizleyin

Temel olarak bir resim tablosu.

Her şey gayet iyi çalışıyor, ancak içeriği aşağı kaydırırken ve tekrar yükseldiğinde, her öğenin önceki görünümlerini bir saniyeden daha kısa bir süre için gösterir ve ardından uygun resmi tekrar gösterir.

Bunu önlemek veya düzeltmek için ne yapabilirim? Sağladığınız yardım ve/veya rehberlik için şimdiden teşekkür ederiz. adı RecyclerView önceki bakış içeriğini gösterir böylece bellek optimize etmek için incelemeler geri dönüştürür anlaşılacağı gibi

package jahirfiquitiva.project.adapters; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.support.v7.graphics.Palette; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

import jahirfiquitiva.project.activities.WallpapersActivity; 
import com.koushikdutta.async.future.FutureCallback; 
import com.koushikdutta.ion.Ion; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.WeakHashMap; 

import jahirfiquitiva.project.R; 

public class WallpapersAdapter extends RecyclerView.Adapter<WallpapersAdapter.WallsHolder> { 

    public interface ClickListener { 
     void onClick(WallsHolder view, int index, boolean longClick); 
    } 

    private ArrayList<HashMap<String, String>> data; 
    private final Context context; 
    private boolean usePalette = true; 
    private final ClickListener mCallback; 
    private final Map<String, Palette> mPaletteCache = new WeakHashMap<>(); 

    public WallpapersAdapter(Context context, ClickListener callback) { 
     this.context = context; 
     this.data = new ArrayList<>(); 
     this.mCallback = callback; 
    } 

    public void setData(ArrayList<HashMap<String, String>> data) { 
     this.data = data; 
     notifyDataSetChanged(); 
    } 

    @Override 
    public WallsHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     return new WallsHolder(inflater.inflate(R.layout.wallpaper_item, parent, false)); 
    } 

    @Override 
    public void onBindViewHolder(final WallsHolder holder, int position) { 
     Animation anim = AnimationUtils.loadAnimation(context, android.R.anim.fade_in); 
     HashMap<String, String> jsondata = data.get(position); 

     holder.name.setText(jsondata.get(WallpapersActivity.NAME)); 
     final String wallurl = jsondata.get(WallpapersActivity.WALL); 
     holder.wall.startAnimation(anim); 
     holder.wall.setTag(wallurl); 

     Ion.with(context) 
       .load(wallurl) 
       .asBitmap() 
       .setCallback(new FutureCallback<Bitmap>() { 
        @Override 
        public void onCompleted(Exception e, Bitmap result) { 
         holder.progressBar.setVisibility(View.GONE); 
         if (e != null) { 
          e.printStackTrace(); 
         } else if (holder.wall.getTag() != null && holder.wall.getTag().equals(wallurl)) { 
          holder.wall.setImageBitmap(result); 
          if (usePalette) { 
           Palette p; 
           if (mPaletteCache.containsKey(wallurl)) { 
            p = mPaletteCache.get(wallurl); 
           } else { 
            p = new Palette.Builder(result).generate(); 
            mPaletteCache.put(wallurl, p); 
           } 
           if (p != null) { 
            Palette.Swatch wallSwatch = p.getVibrantSwatch(); 
            if (wallSwatch != null) { 
             holder.titleBg.setBackgroundColor(wallSwatch.getRgb()); 
             holder.titleBg.setAlpha(1); 
             holder.name.setTextColor(wallSwatch.getTitleTextColor()); 
             holder.name.setAlpha(1); 
            } 
           } 
          } 
         } 
        } 
       }); 
    } 

    @Override 
    public int getItemCount() { 
     return data.size(); 
    } 

    public class WallsHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 

     public final View view; 
     public final ImageView wall; 
     public final TextView name; 
     public final ProgressBar progressBar; 
     public final LinearLayout titleBg; 

     WallsHolder(View v) { 
      super(v); 
      view = v; 
      wall = (ImageView) v.findViewById(R.id.wall); 
      name = (TextView) v.findViewById(R.id.name); 
      progressBar = (ProgressBar) v.findViewById(R.id.progress); 
      titleBg = (LinearLayout) v.findViewById(R.id.titlebg); 

      view.setOnClickListener(this); 
      view.setOnLongClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      int index = getLayoutPosition(); 
      if (mCallback != null) 
       mCallback.onClick(this, index, false); 
     } 

     @Override 
     public boolean onLongClick(View v) { 
      int index = getLayoutPosition(); 
      if (mCallback != null) 
       mCallback.onClick(this, index, true); 
      return false; 
     } 
    } 
} 

cevap

3

:

Bu

adaptör kodudur. Görüntüyü internetten yüklediğinizden, görüntüyü yüklemek için çok az zaman harcarsınız, böylece önceki görüntünün içeriği gözlenebilir. Aşağıdaki şeylerden birini yapabilirsiniz.

1) yükleme öncekilerin/görünmez ImageView görünürlüğünü) bu

//load default image first     
holder.wall.setImageResource(R.id.your_default_image_resource); 
//load actual image 
Ion.with(context) 
      .load(wallurl) 
    .asBitmap() 
    .setCallback(new FutureCallback<Bitmap>() { 
     @Override 
     public void onCompleted(Exception e, Bitmap result) { 
      holder.progressBar.setVisibility(View.GONE); 
      if (e != null) { 
       e.printStackTrace(); 
      } else if (holder.wall.getTag() != null && holder.wall.getTag().equals(wallurl)) { 
       holder.wall.setImageBitmap(result); 
       if (usePalette) { 
        Palette p; 
        if (mPaletteCache.containsKey(wallurl)) { 
         p = mPaletteCache.get(wallurl); 
        } else { 
         p = new Palette.Builder(result).generate(); 
         mPaletteCache.put(wallurl, p); 
        } 
        if (p != null) { 
         Palette.Swatch wallSwatch = p.getVibrantSwatch(); 
         if (wallSwatch != null) { 
          holder.titleBg.setBackgroundColor(wallSwatch.getRgb()); 
          holder.titleBg.setAlpha(1); 
          holder.name.setTextColor(wallSwatch.getTitleTextColor()); 
          holder.name.setAlpha(1); 
         } 
        } 
       } 
      } 
     } 
    }); 

2 gibi memory.something korumak için, gerçek görüntü yüklemeden önce, tercihen küçük boy lokal kaynaktan bir varsayılan görüntü ayarlama görüntü yüklendikten sonra görüntüyü tekrar VISIBLE yapın.

//hide the imageview 
holder.wall.setVisibility(View.INVISIBLE); 
Ion.with(context) 
      .load(wallurl) 
    .asBitmap() 
    .setCallback(new FutureCallback<Bitmap>() { 
     @Override 
     public void onCompleted(Exception e, Bitmap result) { 
      holder.progressBar.setVisibility(View.GONE); 
      if (e != null) { 
       e.printStackTrace(); 
      } else if (holder.wall.getTag() != null && holder.wall.getTag().equals(wallurl)) { 
       //show the imageview and set bitmap 
       holder.wall.setVisibility(View.VISIBLE); 
       holder.wall.setImageBitmap(result); 
       if (usePalette) { 
        Palette p; 
        if (mPaletteCache.containsKey(wallurl)) { 
         p = mPaletteCache.get(wallurl); 
        } else { 
         p = new Palette.Builder(result).generate(); 
         mPaletteCache.put(wallurl, p); 
        } 
        if (p != null) { 
         Palette.Swatch wallSwatch = p.getVibrantSwatch(); 
         if (wallSwatch != null) { 
          holder.titleBg.setBackgroundColor(wallSwatch.getRgb()); 
          holder.titleBg.setAlpha(1); 
          holder.name.setTextColor(wallSwatch.getTitleTextColor()); 
          holder.name.setAlpha(1); 
         } 
        } 
       } 
      } 
     } 
    }); 
+0

İlk seçenek çalıştı. İkincisi hala bana aynı sorunu verdi. Yardım için teşekkürler. –

0

Ben böyle bir yer sahibi eklemek gerektiğini düşünüyorum:

Ion.with(context).load("http://example.com/image.png") 
    .withBitmap() 
    .placeholder(R.drawable.placeholder_image) 
    .error(R.drawable.error_image) 
    .intoImageView(imageView); 

veya set varsayılan görüntü ilk başta.

holder.wall.setImageResource(R.drawable.placeholder_image); 
2

Üzerine Yaz onViewRecycled (VH holder) görüntü null ayarlamak için. Bunun gibi

:

public void onViewRecycled (VH holder) { 

    holder.wall.setImageBitmap(null); 
}