Coder
Coder

Reputation: 11

How to load image in imageview only when on screen in RecyclerView

I am trying to make an app that contains unlimited images from storage or web.But problem is recyclerview stucks or scroll very slowly.

How i can make a list of images in my app like gallery, whats app, instagram, etc. ?
And i want run my app very smoothly

Please help me !

Code of Adapter :

public class Recyclerview1Adapter extends RecyclerView.Adapter<Recyclerview1Adapter.ViewHolder> {
      ArrayList<HashMap<String, Object>> _data;
      public Recyclerview1Adapter(ArrayList<HashMap<String, Object>> _arr) {
          _data = _arr;
      }
      
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View _v = _inflater.inflate(R.layout.custom, null);
          return new ViewHolder(_v);
      }
      
      @Override
      public void onBindViewHolder(ViewHolder _holder, final int _position) {
          View _view = _holder.itemView;
          
          final LinearLayout linear1 = (LinearLayout) _view.findViewById(R.id.linear1);
          final ImageView imageview1 = (ImageView) _view.findViewById(R.id.imageview1);
          final TextView textview1 = (TextView) _view.findViewById(R.id.textview1);
          
          textview1.setText(_data.get((int)_position).get("image-name").toString());
          imageview1.setImageBitmap(FileUtil.decodeSampleBitmapFromPath(_data.get((int)_position).get("image-path").toString(), 1024, 1024));
      }
      
      @Override
      public int getItemCount() {
          return _data.size();
      }
      
      public class ViewHolder extends RecyclerView.ViewHolder{
          public ViewHolder(View v){
              super(v);
          }
      }
      
  }

Upvotes: 1

Views: 602

Answers (1)

Chinmay Bhardwaj
Chinmay Bhardwaj

Reputation: 111

You need to explain clearly the issue with proper description.

As per my understanding, you are fetching all the images at once and passing them to the recyclerview adapter. This might cause scrolling issue if there are lots of images.

You need to load limited images at run time using paging. Load new images only when user has reached end of recyclerview.

Have a look at the official Paging documentation or this GitHub library.

Upvotes: 1

Related Questions