JerryLi
JerryLi

Reputation: 99

RecyclerView load 10000+ image from backend api

I get JSON data in my back-end API.

[
  {
    id: "is random"
    image: "my url"
  },
  ......10000+
]

My id is random number without continuity.

Each json object has image.

I call api and put data in RecyclerView .

But when RecyclerView is displayed, it will get stuck or crash.

I use Glide load image.

GlideApp.with(context).load(myUrl).placeholder(myPlaceholder).into(uiImage)

How can I let RecyclerView read it smoothly?

Can I use Paging Library to let it display five data at the beginning?

But I don't know which kind of DataSource is suitable for my json format.

ItemKeyedDataSource? or PageKeyedDataSource? or PositionalDataSource?

Update:

class MyAdapter(): ListAdapter<Item, ItemHolder>(Item.DIFF_CALLBACK){
   override fun onCreateViewHolder(parent: ViewGroup, type: Int): ItemHolder=
        ItemHolder(LayoutInflater.from(parent.context), parent).also {}

   override fun onBindViewHolder(holder: ItemHolder, position: Int): Unit = 
     with(holder) {
        val item = getItem(position)
        uiTextView.text = item.id
        GlideApp.with(context).load(item.image).placeholder(myPlaceholder).into(uiImage)
    }
}

Update:

class ItemHolder(inflater: LayoutInflater, parent: ViewGroup)
    : RecyclerView.ViewHolder(inflater.inflate(R.layout.my_Item, parent, false)) {
    val uiTextView: TextView = itemView.findViewById(R.id.my_textView)
    val uiImage: ImageView = itemView.findViewById(R.id.my_image)
}

Sometimes it crashes if I remove the placeholder.

GlideApp.with(context).load(myUrl).into(uiImage)

Log message:

I/Choreographer: Skipped 3660 frames!  The application may be doing too much work on its main thread.

Upvotes: 0

Views: 679

Answers (1)

Sandip Savaliya
Sandip Savaliya

Reputation: 784

inject GlideApp, instead of creating new instance every-time, which will make your app more smooth.

And also if you are using Paging Library, then use PagedListAdapter.

Upvotes: 1

Related Questions