Load all items in android RecyclerView at once

Viewed 8736

I'm too tired for searched this question in two day's , but i got false answer , I implement RecyclerView and it worked well, but my RecycleView have many items like photo's and video's like Instagram . it mean my adapter will invoke onBindViewHolder for all items to start download and loading image's and video's . but RecycleView just invoked onBindViewHolder for each item's when item is visible (scroll show it) . it's impossible ? if not how instagram can do that . pay attention : i don't want to using database for save image's and video's

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {

    Log.e("position",position+" ");

    thread t = new Thread(new Runnable(){
       public void run(){
           Bitmap bitmap = downloadPhoto();//it mean photo downloaded and converted to Bitmap
           runOnUiThread(new Runnable(){
               holder.imageView.setImageBitmap(bitmap);
           ));
       }

    });
    t.start();

}
3 Answers

You can try embedding the recyclerview inside nested scroll view and do not forget to disable nested scrollview of recyclerview. This way all items will be loaded at once even though it violates the purpose of the recyclerview.

Although I wouldn't recommend you to use this "solution", since it destroys the original purpose of using a RecyclerView, it is possible to let the RecyclerView invoke onBindViewHolder for every item at once. For it to work, just set the RecyclerView inside a NestedScrollView.

As you describe, you only want to load photos when the item is visible. In fact RecyclerView adapter will only invoke onBindViewHolder when the item appears(visible) so you can start download photos then update UI when photo ready for you. And you now do not load all item's photo at once, you just load when you need.

ps: If the item's photo url is known, you can use Glide or Picasso library.

Related