I have a RecyclerView, when scrolling the screen on the screen, the photos of the upper screen are first displayed, and only then the correct photos are loaded. I do not know how to solve this problem.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private final Activity context; private final ArrayList<Folder> FOLDERS; View view; public long getItemId(int position) { return position; } @Override public int getItemCount() { Log.wtf("TAG","Folders size: "+ FOLDERS.size()); return FOLDERS.size(); } public int getPosition(int position) { return position; } //bitmap optimisation public static 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 > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } public static Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight) { // 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; return BitmapFactory.decodeFile(path, options); } public void loadBitmap(String path, ImageView imageView) { BitmapWorkerTask task = new BitmapWorkerTask(imageView); task.execute(path); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public TextView title; ImageView image1; ImageView image2; ImageView image3; ImageView image4; ImageView image5; TextView slides; public ViewHolder(View v) { super(v); title = (TextView) v.findViewById(R.id.item); image1 = (ImageView) v.findViewById(R.id.icon1); image2 = (ImageView) v.findViewById(R.id.icon2); image3 = (ImageView) v.findViewById(R.id.icon3); image4 = (ImageView) v.findViewById(R.id.icon4); image5 = (ImageView) v.findViewById(R.id.icon5); slides = (TextView) v.findViewById(R.id.textView1); } } public MyAdapter(Activity context, ArrayList<Folder> FOLDERS) { this.context = context; this.FOLDERS = FOLDERS; getItemCount(); } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.mylist, parent, false); ViewHolder vh = new ViewHolder(view); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Folder folder = FOLDERS.get(position); ArrayList<String> imgs = folder.getPicturelist(); holder.title.setText(folder.getName()); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 10; for (int i = 0; i < 5; i++) { switch (i) { case 0: loadBitmap(imgs.get(i), holder.image1); break; case 1: loadBitmap(imgs.get(i), holder.image2); break; case 2: loadBitmap(imgs.get(i), holder.image3); break; case 3: loadBitmap(imgs.get(i), holder.image4); break; case 4: loadBitmap(imgs.get(i), holder.image5); break; } } holder.slides.setText("Количество слайдов: " + imgs.size()); view.setTag(holder); } } AsyncTask code
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> { private final WeakReference<ImageView> viewHolderWeakReference; private String data; public BitmapWorkerTask(ImageView imageView) { viewHolderWeakReference = new WeakReference<ImageView>(imageView); } @Override protected Bitmap doInBackground(String... params) { Log.i("TAG", "Async task works in background"); data = String.valueOf(params[0]); Log.wtf("Params: ", params[0]); final Bitmap bitmap =decodeSampledBitmapFromResource(data, 30, 30); addBitmapToMemoryCache(String.valueOf(params[0]), bitmap); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { Log.i("onPostExecute", "works!"); if (viewHolderWeakReference != null && bitmap != null) { final ImageView imageView= viewHolderWeakReference.get(); if (imageView != null){ imageView.setImageBitmap(bitmap); } } } public void addBitmapToMemoryCache(String key, Bitmap bitmap) { if (getBitmapFromMemCache(key) == null) { FirstscreenActivity.mMemoryCache.put(key, bitmap); } } public Bitmap getBitmapFromMemCache(String key) { return FirstscreenActivity.mMemoryCache.get(key); } } I also provide the onCreate code
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.front); list = (RecyclerView) findViewById(R.id.list); list.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(getApplicationContext()); mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); getFromSdcardFolders(); list.setLayoutManager(mLayoutManager); mAdapter = new MyAdapter(this, FOLDERS); list.setAdapter(mAdapter); final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getByteCount() / 1024; } };