Lesson 3 - RecyclerView Flashcards
Efficient scrolling; common design patterns.
RecyclerView is?
Efficient way to view, scroll + interact with data.
Problems with scrollView
resource intensive; while could contain all of cells at once, uses a lot of memory + lots of upfront loading+setup. So, RecyclerView.
RecyclerView high-level works how?
what’s visible, plus some of what’s not (those closely off screen) in a queue
Modules around RecyclerView
Layout Manager, RecyclerView, Adapter+ViewHolder, Data Source
Adapter
provide views when needed to RecyclerView
also binds data from some Datasource to the view
sends views to RecyclerView in ViewHolder objects;
(ie view-provider and databinder)
ViewHolder object
contains a reference to the root view object for the item
used to cache view object for the layout to make it less resource intensive
findViewById therefore called only when inflating views, and not when binding views to data
Layout Manager
tells RecyclerView how to layout all its views
steps to RecyclerView
- add dependency
- create RecyclerView in Layout
- create item list layout and ViewHolder
- add RecyclerView adapter
- add LayoutManager, connect everything
sp vs dp
sp scales with user preferences
use of ViewHolder
extend base RecyclerView.ViewHolder,
add property for view,
populate view property in constructor (parameter type View cast to specific type of property’s view),
bind method to set data for view
String from int Java
String.valueOf()
Adapter does; 3 overrides
- create new items
- populates items with data
- returns information
overrides:
1. onCreateViewHolder; called when recyclerview instantiates new Viewholder instance
2. onBindViewHolder; called when Recyclerview wants to populate the view with data from our model, so that the user can see it, effectively binding it to datasource
3. getitemcount, returns number of items in data source
also inflates view
what is inflation?
“Inflation” is a term that refers to parsing XML and turning it into UI-oriented data structures. You can inflate a view hierarchy, a menu structure, and other types of resources. Often this is done behind the scenes by the framework (when you call setContentView(R.layout.main), for instance). A typical case when you explicitly inflate something yourself is when creating menus, as described in the guide subject Creating Menus.
code to inflate for MyViewHolder
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
// TODO (7) Override onBindViewHolder // TODO (8) Within onBindViewHolder, call holder.bind and pass in the position View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately); NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder; }
viewholder
determines how an individual entry is displayed