What is the difference between
layoutinflater.inflate()
andlayoutinflater.from()
?Why does
layoutinflater.from()
is used inonCreateViewHolder()
inRecyclerView
?What is the purpose of
getItemCount()
inRecyclerView
?

- 6,405
- 16
- 66
- 120

- 4,532
- 5
- 31
- 53
3 Answers
layoutinflater.inflate used to inflate the xml layout through activity or fragment in which already you have context of the activity
layoutinflater.from used to get the get the context of the Layout inflater where you want to inflate the xml layout (ex to access the xml resource file in adapter class you need on context of the activity)

- 80
- 2
layoutinflater.inflate vs layoutinflater.from
Here's a link to the documentation
LayoutInflater.from will return a LayoutInflater object from the given context. layoutinflater.inflate will inflate a new view from the resource and let you attach it to a parent also.Why does layoutinflater.from is used in onCreateViewHolder method in Recycler view ?
This is done so that you can get a reference to the layoutinflater of your context (i.e. your activity) and use that reference to then call the inflate() method where you specify the id of the layout xml of your recyclerview item.What is the pupose of getItemCount method in recycler view ?
Check out the documentation
This method is useful if you don't have a direct reference to the dataset backing your adapter.

- 47
- 1
- 8
What is the difference between
layoutinflater.inflate
andlayoutinflater.from
?
Semantically they are the same, just syntactic sugar.
Why does
layoutinflater.from
is used inonCreateViewHolder
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 inrecyclerView
?
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.

- 28,539
- 11
- 83
- 129