Abhishek Kumar Abhishek Kumar - 3 months ago 14
Android Question

difference between layoutinflater.inflate and layoutinflater.from


  1. What is the difference between
    layoutinflater.inflate()
    and
    layoutinflater.from()
    ?

  2. Why does
    layoutinflater.from()
    is used in
    onCreateViewHolder()
    in
    RecyclerView
    ?

  3. What is the purpose of
    getItemCount()
    in
    RecyclerView
    ?


Answer

What is the difference between layoutinflater.inflate and layoutinflater.from?

Semantically they are the same, just syntactic sugar.

Why does layoutinflater.from is used in onCreateViewHolder method in RecyclerView?

In onCreateViewHolder method, you should instantiate a place-holding view, which later will be used to display a row. If you want to instantiate the row view from a xml file, you should inflate that file which gives a view. The inflation process is done through a LayoutInflater object. If you don't want to use a xml, you won't need that inflater.

What is the purpose of getItemCount method in recyclerView?

Without this, how would that recycler view know how many rows this list would have? The recycler view first calls this method and subsequently calls onCreateViewHolder and getItem methods based on it.