Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 3.21 KB

mvc_simple_recycler_view_tutorial.md

File metadata and controls

60 lines (46 loc) · 3.21 KB

Simple RecyclerView use in MVC

Overview

This tutorial briefly describes the use of RecyclerViews in an MVC component. For the prerequisites to this tutorial, see Additional Resources.

Using RecyclerViews in a basic way in MVC is now very similar to how basic non-recycler lists work. The general flow is as follows:

  • Build and maintain a ModelList.
  • Create a SimpleRecyclerViewAdapter.
  • Register each type of view with the adapter.
  • Clean up recycled views if necessary.

Both the ModelListAdapter and the SimpleRecyclerViewAdapter implement the MVCListAdapter interface. That is to say that usage of the two adapters is roughly identical. The main reason to use one or the other is dependent on the capabilities needed by the views that each adapter is attached to. Changing from using a ListView to a RecyclerView should be simple once the adapter is implemented.

Additional Resources

Instantiating a SimpleRecyclerViewAdapter

A SimpleRecyclerViewAdapter is instantiated the same way a ModelListAdapter is; you only need to provide a handle to the ModelList for the data the view will display.

Fast view lookup

Typical usage of RecyclerView requires the creation of a ViewHolder to cache the views that frequently have new content bound to them. The SimpleRecyclerViewAdapter does away with this concept.

A generic solution to fast view lookup can be found in ViewLookupCachingFrameLayout. This version of a FrameLayout exposes a method: fastFindViewById. The added method attempts to find the specified view, and if it exists, it will be cached for faster lookups. See the full documentation for this class here.

Cleaning up a recycled view

In some cases complex views can hold on to expensive resources; the most common cases is holding a reference to a large bitmap. The SimpleRecyclerViewAdapter does not explicitly provide a way to do resource cleanup, but this can still be achieved with RecyclerView#RecyclerListener.

recyclerListener = (holder) -> {
    // Do cleanup for |holder| here. The root view can be accessed via |holder#itemView|
};

// Optionally the listener can be defined in-line below.
mRecyclerView.setRecyclerListener(recyclerListener);