Skip to content

Commit

Permalink
Add documentation for SimpleRecyclerViewAdapter usage
Browse files Browse the repository at this point in the history
Change-Id: I96d4b52033828c8e9bb62fc184827da20bbe7b9e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1732795
Reviewed-by: Theresa  <twellington@chromium.org>
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#684011}
  • Loading branch information
iotitan authored and Commit Bot committed Aug 5, 2019
1 parent 497abfc commit 80c40c5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/ui/android/mvc_simple_list_tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ In this example we'll be creating a simple menu list where each list item consis
label.

### Additional Resources
[Introductory MVC tutorial][mvc_tutorial_link]
* [Introductory MVC tutorial][mvc_tutorial_link]
* [Expanding to RecyclerViews in MVC][mvc_recycler_view_tutorial]

### File Structure
The file structure of our component will be the following:
Expand Down Expand Up @@ -168,4 +169,5 @@ class SimpleMenuItemViewBinder {
```

[mvc_tutorial_link]:https://chromium.googlesource.com/chromium/src/+/HEAD/docs/ui/android/mvc_architecture_tutorial.md
[Expanding to RecyclerViews in MVC]:https://chromium.googlesource.com/chromium/src/+/HEAD/docs/ui/android/mvc_simple_recycler_view__tutorial.md

60 changes: 60 additions & 0 deletions docs/ui/android/mvc_simple_recycler_view_tutorial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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](#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```][model_list_link].
* Create a [```SimpleRecyclerViewAdapter```][simple_rva_link].
* 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
* [Introductory MVC tutorial][mvc_tutorial_link]
* [Simple MVC list tutorial][mvc_list_tutorial_link]

### 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```][fast_lookup_frame_layout_link]. 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][fast_lookup_frame_layout_link].

### 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```.

```java
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);

````

[model_list_link]:(https://codesearch.chromium.org/chromium/src/ui/android/java/src/org/chromium/ui/modelutil/MVCListAdapter.java?rcl=d22c9731463bad77645cb0f1a928dec7da79bff9&l=38)
[simple_rva_link]:(https://codesearch.chromium.org/chromium/src/ui/android/java/src/org/chromium/ui/modelutil/SimpleRecyclerViewAdapter.java)
[mvc_tutorial_link]:https://chromium.googlesource.com/chromium/src/+/HEAD/docs/ui/android/mvc_architecture_tutorial.md
[mvc_list_tutorial_link]:https://chromium.googlesource.com/chromium/src/+/HEAD/docs/ui/android/mvc_simple_list_tutorial.md
[fast_lookup_frame_layout_link]:https://codesearch.chromium.org/chromium/src/ui/android/java/src/org/chromium/ui/widget/ViewLookupCachingFrameLayout.java

0 comments on commit 80c40c5

Please sign in to comment.