Search code examples
androidandroid-recyclerviewlag

Laggy Recycler View without even any code?


I was using recycler view to List Images but it was getting laggy after even caching and all. So I Decided to use Glide library but still laggy as hell. I finally thought to check with just a single drawable and still it's laggy. I don't get why. Please help. Here is the code. There is not much code still. You can see the code I was using for my Image Viewing commented out.

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageHolder>
{
private File file;
private String logging=getClass().getSimpleName();
private  int size;
private MemCache memCache;
private  Context context;
private Bitmap bitmap;
public ImageAdapter(File file,int Size,MemCache memCache,Context context)
{
    this.file = file;
    size=Size;
    this.memCache=memCache;
    this.context=context;
    bitmap=BitmapFactory.decodeResource(context.getResources(),R.drawable.empty_photo);
}


@Override
public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    return new ImageHolder(new ImageView(context),size);
}

@Override
public void onBindViewHolder(ImageHolder holder, int position)
{
    Glide.with(context).load(R.drawable.empty_photo).into(holder.getImageView());
//        if (memCache.get(file.listFiles()[position].toString())!=null)
//        {
//            holder.getImageView().setImageBitmap(memCache.get(file.listFiles()[position].toString()));
//        }
//        else
//        {
//            ImageSyncTask imageSyncTask = new ImageSyncTask(size, holder.getImageView(),memCache);
//            imageSyncTask.executeOnExecutor(imageSyncTask.THREAD_POOL_EXECUTOR, file.listFiles()[position]);
//        }
//        Glide.with(context).load(file.listFiles()[position]).crossFade().into(holder.getImageView());
}
@Override
public int getItemCount()
{
    if (file!=null&&file.listFiles()!=null) return file.listFiles().length;
    else return 0;
}













public static class ImageHolder extends RecyclerView.ViewHolder
{
    private ImageView imageView;
    public ImageHolder(View itemView,int size)
    {
        super(itemView);
        imageView=(ImageView)itemView;
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setMinimumWidth(size);
        imageView.setMinimumHeight(size);
    }
    public ImageView getImageView()
    {
        return imageView;
    }
    public void clearImage()
    {
        imageView.setImageResource(android.R.color.transparent);
    }


}

}

I know there is one loading Bitmap but still its just one. That shouldn't make that much lag. And yes I have used the typical setImageBitmap instead of Glide but still laggy. There is the view holder. Just Simple function. And I was previously using a proper layout for it but it was not working as well. In this I have just used New ImageView() and setParamaters just to make sure if there was a problem in Layout.

Please Help. I don't get why the typical Adapter is creating Lag.

Original MainActivity

public class MainActivity extends AppCompatActivity
{
RecyclerView recyclerView;
int cnum;
private MemCache memCache;
private String logging=getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DisplayMetrics displayMetrics=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int maxmem=(int)Runtime.getRuntime().maxMemory()/1024;
    int cache_mem=maxmem/10;
    memCache=new MemCache(cache_mem);
    int orientation=getResources().getConfiguration().orientation;
    if (orientation== Configuration.ORIENTATION_PORTRAIT)
    cnum=3;
    else cnum=5;
    recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),cnum));
    recyclerView.setAdapter(new ImageAdapter(getGallery(),displayMetrics.widthPixels/cnum,memCache,getApplicationContext()));
}

private File getGallery()
{
    return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath(),"Trial");
}

}

I have also tried and removed maximum of calls which can be removed to make sure there is not much task on UI thread.


Solution

  • Doing disk I/O on the main application thread is a common source of "jank", as that I/O may take a bit of time. StrictMode can help you determine where you are doing disk I/O on the main application thread.

    In general, you want to load your model data (e.g., a list of files) on a background thread, then work off of the in-memory representation of that model data on the main application thread.