0

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.
piet.t
  • 11,718
  • 21
  • 43
  • 52
JerryLi
  • 99
  • 1
  • 3
  • 8

1 Answers1

1

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.

Sandip Savaliya
  • 784
  • 6
  • 19
  • 1
    ListAdapter extends Recyclerview.Adapter with DIFF feature. So it works same as Recyclerview.Adapter with some added functionality. https://developer.android.com/reference/android/support/v7/recyclerview/extensions/ListAdapter – rafa Feb 27 '19 at 07:04
  • Thank you so much for updating my knowledge about new ListAdapter. :) @rafa – Sandip Savaliya Feb 27 '19 at 08:13