-1
  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?

Sufian
  • 6,405
  • 16
  • 66
  • 120
Abhishek Kumar
  • 4,532
  • 5
  • 31
  • 53

3 Answers3

1

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)

1
  1. 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.

  2. 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.

  3. 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.

rashmi1412
  • 47
  • 1
  • 8
1

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.

frogatto
  • 28,539
  • 11
  • 83
  • 129