Showing posts with label recyclerview. Show all posts
Showing posts with label recyclerview. Show all posts

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

Friday, September 5, 2014

Android: RecyclerView cannot be resolved to a type

RecyclerView cannot be resolved to a type? How to resolve it?

We need recyclerview-v7 Support Library to solve the issue. but how to get? letc check...

Check whether Recyclerview is existing in the SDK or not because it is a part of the support library.

If you're not find the folder “recyclerview-v7” in the following sdk directory "..\sdk\extras\android\m2repository\com\android\support" then follow the steps otherwise follow from step 3.

Steps to achieve:
  1. Select the latest Android SDK Tools, Platform-tools, and Build-tools.
  2. Select latest Android Support Library & Android Support Repository.
  3. After updating Android Support Repository from SDK Manager go to..\sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0-rc1
  4. Unzip recyclerview-v7-21.0.0-rc1.aar
  5. After unzipping recyclerview-v7-21.0.0-rc1.aar you will get classes.jar
  6. rename classes.jar to recyclerView.jar for feature reference and add to your Android app under /lib folder
Overview:
RecyclerView updates and replaces ListView. It's claimed to be easier to use and feature performance improvements in dynamic views.

RecyclerView is easy to use, because it provides:
  • A layout manager for positioning items
  • Default animations for common item operations

The most important classes of the RecyclerView API.

Class
Usage
Adapter
The Adapter is also responsible for making a View for each item in the data set.
LayoutManager
A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible to the user.
ItemAnimator
Animations for adding and removing items are enabled by default in RecyclerView



Thanks for reading :)
Any suggestions can be added in comments. feedback are welcome.