RecyclerView is a more advanced version of List View with improved performance and additional features.
A subclass of RecyclerView.Adapter responsible for providing views that represent items in a data set
The position of a data item within an Adapter
The index of an attached child view as used in a call to getChildAt(int). Contrast with Position
The process of preparing a child view to display data corresponding to a position within the adapter
A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction
A child view that has entered into a temporarily detached state during layout. Scrap views may be reused without becoming fully detached from the parent RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the view was considered dirty
A child view that must be rebound by the adapter before being displayed
The RecyclerView is a flexible view for providing a limited window into a large data set.
Before using the RecyclerView you have to add the support library dependency in the build.gradle file:
You can find latest version number of recyclerview from official site.
Other related topics:
There are other topics which describe the RecyclerView components:
Add the dependency as described in the Remark section, then add a RecyclerView to your layout:
Once you have added a RecyclerView widget to your layout, obtain a handle to the object, connect it to a layout manager and attach an adapter for the data to be displayed:
Or simply setup layout manager from xml by adding this lines:
If you know that changes in content of the RecyclerView won't change the layout size of the RecyclerView, use the following code to improve the performance of the component. If RecyclerView has a fixed size, it knows that RecyclerView itself will not resize due to its children, so it doesn’t call request layout at all. It just handles the change itself. If invalidating whatever the parent is, the coordinator, layout, or whatever. (you can use this method even before setting LayoutManager and Adapter):
RecyclerView provides these built-in layout managers to use. So you can create a list, a grid and a staggered grid using RecyclerView:
Add an adapter and call .notifyDataSetChanged(); as usual !
This is not an inbuilt feature of Recyclerview but added in the support libraries. So don't forget to include this in your app level build.gradle file
Multiple ItemDecorations can be added to a single RecyclerView.
Changing divider color :
It's pretty easy to set an color for a itemDecoration.
step is: creating a divider.xml file which is located on drawable folder
While the above answer will work you can use this approach as well using a recycler view using a NestedScrollView .You can add a layout for header using the following approach:
Or you may also use a LinearLayout with vertical alignment in your NestedScrollView.
Note: This will only work with RecyclerView above 23.2.0
Animate data change
RecyclerView will perform a relevant animation if any of the "notify" methods are used except for notifyDataSetChanged; this includes notifyItemChanged, notifyItemInserted, notifyItemMoved, notifyItemRemoved, etc.
The adapter should extend this class instead of RecyclerView.Adapter.
You should NOT use the same List for setModels and List in the adapter.
You declare models as global variables. DataModel is a dummy class only.
Initialize models before pass it to adapter. YourAdapter is the implementation of AnimatedRecyclerAdapter.
Call this after you have updated your global models.
If you do not override equals, all the comparison is compared by reference.
Example using SortedList
Android introduced the SortedList class soon after RecyclerView was introduced. This class handles all 'notify' method calls to the RecyclerView.Adapter to ensure proper animation, and even allows batching multiple changes, so the animations don't jitter.
Drag&Drop and Swipe with RecyclerView
You can implement the swipe-to-dismiss and drag-and-drop features with the RecyclerView without using 3rd party libraries.
Just use the ItemTouchHelper class included in the RecyclerView support library.
Instantiate the ItemTouchHelper with the SimpleCallback callback and depending on which functionality you support, you should override onMove(RecyclerView, ViewHolder, ViewHolder) and / or onSwiped(ViewHolder, int)and and finally attach to your RecyclerView.
It's worth mentioning that SimpleCallback constructor applies the same swiping strategy to all items in the RecyclerView. It's possible in any case to update the default swiping direction for specific items by simply overriding method getSwipeDirs(RecyclerView, ViewHolder).
Let's suppose for example that our RecyclerView includes a HeaderViewHolder and that we obviously don't want to apply swiping to it. It will be enough to override getSwipeDirs as follows:
Endless Scrolling in Recycleview.
Here I have shared a code snippet for implementing endless scrolling in recycle view.
First make a one abstract method in Recycleview adapter like below.
Now override onBindViewHolder and getItemCount() method of ViewAllCategoryAdapter class and call Load() method like below.
Now every backend logic is complete now it's time to execute this logic.It's simple you can override load method where you create object of your adapter.this method is automatically call while user reach at end of the listing.
Now load() method automatically call while user scroll at end of list.
Filter items inside RecyclerView with a SearchView
add filter method in RecyclerView.Adapter:
itemsCopy is initialized in adapter's constructor like itemsCopy.addAll(items).
If you do so, just call filter from OnQueryTextListener from SearchView:
Popup menu with recyclerView
put this code inside your ViewHolder
note: In this code I am using btnExpand click-event, for whole recyclerview click event you can set listener to itemView object.
alternative way to show icons in menu
Here is the output:
RecyclerView with DataBinding
Here is a generic ViewHolder class that you can use with any DataBinding layout. Here an instance of particular ViewDataBinding class is created using the inflated View object and DataBindingUtil utility class.
After creating this class you can use the <layout> in your layout file to enable databinding for that layout like this:
file name: my_item.xml
and here is your sample dataModel:
By default, Android Data Binding library generates a ViewDataBinding class based on the layout file name, converting it to Pascal case and suffixing "Binding" to it. For this example it would be MyItemBinding for the layout file my_item.xml. That Binding class would also have a setter method to set the object defined as data in the layout file(ItemModel for this example).
Now that we have all the pieces we can implement our adapter like this:
Show default view till items load or when data is not available
I'm using FontAwesome with Iconics Library for the images. Add this to your app level build.gradle file.
Smoother loading of items
If the items in your RecyclerView load data from the network (commonly images) or carry out other processing, that can take a significant amount of time and you may end up with items on-screen but not fully loaded. To avoid this you can extend the existing LinearLayoutManager to preload a number of items before they become visible on-screen:
Using several ViewHolders with ItemViewType
Sometimes a RecyclerView will need to use several types of Views to be displayed in the list shown in the UI, and each View needs a different layout xml to be inflated.
For this issue, you may use different ViewHolders in single Adapter, by using a special method in RecyclerView - getItemViewType(int position).
Below is example of using two ViewHolders:
A ViewHolder for displaying list entries
A ViewHolder for displaying multiple header views
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0