ANDROID RECYCLERVIEW

It’s week eight at Moringa School and as I said in my first blog, I had a lot to learn and yes, I have done that and so I share with you a piece of the many things in my head, the Android Recyclerview.

Android like many other mobile platforms  is able to display a large set of information to users through the small window of a phone or tablet since its inception. The Recycler view is what is used to do this since introduction of Lollipop. It makes heavy use of a development design pattern called the Adapter Pattern and steals some elements from the commonly known Model-View-Controller pattern to map scrolling lists of data as views on the screen. Prior to this,  ListView was used but the advantage of the recyclerview is, it is  more advanced and flexible.

What is Recyclerview?

RecyclerView is an “upgraded” version of ListView which is more advance and flexible.

RecyclerView is a container for displaying large data sets that can be scrolled efficiently by maintaining a limited number of views.

RecyclerView also provides some default animations for common item operations like inserting or removing an item.

Recyclerview Architecture

Recyclerview Architecture

Data Model -> Represents one single item in a list e.g

data model

Adapter -> Pulls the items in the data model and presents items on the screen. i.e.(Content View) via the view Holder. It’s also responsible for creating view for each item and replaces new content for the view whenever the original items is no longer visible.

ViewHolder-> it is the xml layout for the one single item inflated/converted into java code. I.e. It consists of a simple class that holds the references to the UI components all the time the system shows a row in the list.

LayoutManager -> Enables one to customize how the items will be scrolled or arranged on the screen. There are three built in LayoutManager in a recyclerview

  • LinearLayoutManager:-lays items in a list

linear

  • GridLayoutManager:- lays out all items in a grid

grid

  • StaggeredGridLayoutManager:- lays out all items in a staggered grid formation, supports horizontal and vertical layouts and is able to lay out items in reverse.

staggered

  • Item Animator:- Helps one do animations on the recyclerview which was tricky and hard in a list view.
  • Item Decorator:- Used in sectioning recyclerview into different sections.

Summary

According to the English oxford  dictionary, the word recycle means re-use and that is what the recyclerview does. An example is, when one has a list of items lets say eight on the screen, and scrolls so that one gets out, seven remain. the recycler will get that one element and then pass it to the adapter. The goal of the adapter in this case is to reuse the item number one, that is in the recycler. Instead of one having to create a new view item, this item in the recycler is pushed down to the screen as item number eight.

To use recycler view one has to declare the following dependancy:-

dependencies  {  compile ‘com.android.support:appcompat-v7:21.0.0’
compile ‘com.android.support:recyclerview-v7:21.0.+’
}

To use RecyclerView, you have to create 2 essential components:

–   LayoutManager: positions items in RecyclerView and determines when to reuse item views that are no longer visible to the user.

–   RecyclerView.Adapter: provides access to data sets items, creates views for items and binds items info to item view.

Visit my github repo here, fork and clone africanshort stories to see how I implemented the recyclerview.

References

A Guide to Android RecyclerView and CardView. 2015. [Android] CardView and RecyclerView in MaA Guide to Android RecyclerView and CardViewterial Design. [ONLINE] Available at: http://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156. [Accessed 13 May 15].

[Android] CardView and RecyclerView in Material Design Ice Tea 09. 2015. [Android] CardView and RecyclerView in Material Design. [ONLINE] Available at: http://icetea09.com/blog/2014/12/19/android-cardview-and-recyclerview-in-material-design/. [Accessed 13 May 15].

TechSmith. 1995. Devc. [ONLINE] Available at: http://blogs.techsmith.com/inside-techsmith/devcorner-android-lollipop/. [Accessed 13 May 15].

Advertisements