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
layout manager
determines how the collection of items is displayed
layout manager types
linear, grid, staggered grid
using recycler view and adapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO (4) Use findViewById to store a reference to the RecyclerView in mNumbersList mNumbersList = (RecyclerView) findViewById(R.id.rv_numbers);
// TODO (5) Create a LinearLayoutManager variable called layoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); // TODO (6) Use setLayoutManager on mNumbersList with the LinearLayoutManager we created above mNumbersList.setLayoutManager(layoutManager);
// TODO (7) Use setHasFixedSize(true) to designate that the contents of the RecyclerView won't change an item's size mNumbersList.setHasFixedSize(true);
// TODO (8) Store a new GreenAdapter in mAdapter and pass it NUM_LIST_ITEMS mAdapter = new GreenAdapter(NUM_LIST_ITEMS);
// TODO (9) Set the GreenAdapter you created on mNumbersList mNumbersList.setAdapter(mAdapter); } }
adding interface for onClick listener
- declare interface as part of adapter class
- add member of onClickListener that adopts interface
- in constructor, accept an onClickListener parameter and set to member
- in adapter’s ViewHolder, implement View.onClickListener (pre-existing interface)
- in adapter’s ViewHolder, when given a view, view.setOnClickListener to this
- override the onClick this gives us, by getting current adapter position and returning onClick
adapter idea
views (ListView, GridView, RecyclerView) need specific information to display the data. This specific information they need is specified by the interface Adapter. An adapter adopts the Adapter interface, and is hooked up to its data source. Adapter can then specify the the view what the view needs, by hooking up the adapter and the view.