Saturday, September 6, 2014

Android L: UI Widgets : RecyclerView

RecyclerView is a more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically.

RecyclerView is easy to use, because it provides:
  • A layout manager for positioning items
  • Default animations for common item operations
You also have the flexibility to define custom layout managers and animations for this widget.

To use the RecyclerView widget, we have to specify an adapter and a layout manager.

LayoutManager

The LayoutManager is probably the most interesting part of the RecyclerView. This class is responsible for the layout of all child views. There is one default implementation available: LinearLayoutManager which you can use for vertical as well as horizontal lists. To create a custom layout, you extend the RecyclerView.LayoutManager class.

// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);


// improve performance if you know that changes in content
// do not change the size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

Animations

Animations for adding and removing items are enabled by default in RecyclerView.
use the RecyclerView.setItemAnimator method for animations, to customize just extend the RecyclerView.ItemAnimator class

Android provide DefaultItemAnimator and we can customize according to our needs.
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

Adapter:

To create an adapter, you extend the RecyclerView.Adapter class.
The RecyclerView.Adapter class contains few notifyItemXXXX()methods. Which help us in modifying the View.

Two methods used in the example are insert and remove and they are
notifyItemInserted(position);
notifyItemRemoved(position);

mRecyclerView.setAdapter(mAdapter);

// insert or remove item from the view. This methods are in the adapter class.

public void addToList(String name) {
int position = 0;
if (mDataset.size() > 1) {
  position = new Random().nextInt(mDataset.size() - 1);
}
mDataset.add(position, name);
notifyItemInserted(position);
}

public void removeItemFromList(String name) {
int position = mDataset.indexOf(name);
if (position != -1) {
  mDataset.remove(position);
  notifyItemRemoved(position);
}
}



Source Code
You can download the source code by clicking here: Sample RecyclerView .  This project is built using eclipse IDE. Unzip and import the project into Eclipse, it’s a good idea to use the Project by clean and rebuild from the project menu.

Screenshot:
Initial screen with four items.

Added two rows to the recyclerView
Removed one item which is added from RecyclerView
Thanks for reading :) 
Whether this post is helpful?


Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments and feedback are welcome

No comments :

Post a Comment