Skip to content

Instantly share code, notes, and snippets.

@getsadzeg
Last active September 12, 2018 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getsadzeg/aaab99b96c3fd4396ce806c42c072130 to your computer and use it in GitHub Desktop.
Save getsadzeg/aaab99b96c3fd4396ce806c42c072130 to your computer and use it in GitHub Desktop.
What does Adapter do? Understanding RecyclerView

Adapter class is a helper to RecyclerView. It has three responsibilities: To return how many items should be in RecyclerView, inflate item views from XML and return new ViewHolder instance(object). and populate them with appropriate data(Basically, everything is in this order). So, it has (constructor and) three methods:

Constructor:

public MyAdapter(int numberOfItems, ListItemClickListener onClickListener)

where ListItemClickListener is an interface, which has one void methoid defined, onListItemClick.

ViewHolder OnCreateViewHolder(params)

This method inflates item XML and wraps it into ViewHolder.

void onBindViewHolder(params)

This method binds/populates view with data. I also have to mention that ViewHolder class must contain void bind(params), which is then called inside onBindViewHolder.

int getItemCount()

This method returns number of items from data source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment