Mobile Development 23 min read

Mastering RecyclerView: From Basics to Advanced Features

This article provides a comprehensive guide to Android's RecyclerView, covering its core concepts, differences from ListView, standard implementation steps, custom adapters, item decorations, layout managers, animators, header/footer integration, empty views, drag-and-swipe handling, nested scrolling, and performance optimizations, with code examples and diagrams.

Tencent TDS Service
Tencent TDS Service
Tencent TDS Service
Mastering RecyclerView: From Basics to Advanced Features

Introduction

RecyclerView is a new UI component introduced in Android 5.0 that can replace the traditional ListView.

Android ListView vs RecyclerView Comparison – Caching Mechanism

Today we will explore all aspects of RecyclerView that you need to know.

Basic Concepts

RecyclerView is part of the support‑v7 library and can be added with:

compile 'com.android.support:recyclerview-v7:24.2.1'

The official definition is:

A flexible view for providing a limited window into a large data set.

Flexibility is a key characteristic, though it shares similarities with ListView.

Why RecyclerView?

RecyclerView does not completely replace ListView; they serve different scenarios. Its introduction allows many custom scrolling components (horizontal ListView, GridView, waterfall layouts) to be deprecated because RecyclerView can implement all of them via LayoutManager.

ListView vs RecyclerView

ListView advantages: addHeaderView(), addFooterView() for header/footer.

Custom dividers via android:divider. setOnItemClickListener() and setOnItemLongClickListener().

These features are not directly available in RecyclerView and must be implemented manually, making ListView simpler for basic displays.

RecyclerView advantages:

Built‑in view recycling without needing if(convertView == null) checks.

Supports partial updates.

Easy to add item add/remove animations.

Supports drag and swipe‑to‑delete.

RecyclerView is a plug‑in architecture that decouples functionality for better extensibility.

Partial Refresh in ListView

ListView updates the whole list via adapter.notifyDataSetChanged(), which repaints every item. To update a single item, you can obtain the child view and its ViewHolder:

Standard Usage

The typical steps to use RecyclerView are:

Create an Adapter extending RecyclerView.Adapter<VH>.

Create a ViewHolder extending RecyclerView.ViewHolder.

Implement onCreateViewHolder(), onBindViewHolder() and getItemCount().

In onCreateViewHolder() you must inflate the layout with:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_1, parent, false);

Do not inflate with a null parent.

Set the Adapter and LayoutManager:

List<String> data = initData();
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new NormalAdapter(data));

RecyclerView provides fine‑grained update APIs such as notifyItemInserted(), notifyItemRemoved(), and notifyItemChanged(), unlike ListView’s global notifyDataSetChanged().

The Four Core Components

Adapter – supplies data to items.

LayoutManager – positions item views.

ItemAnimator – handles add/remove/change animations.

ItemDecoration – draws dividers or offsets between items.

Adapter

Beyond the basic adapter, a universal adapter (QuickAdapter) can reduce boilerplate. It defines:

public abstract class QuickAdapter<T> extends RecyclerView.Adapter<QuickAdapter.VH>

The ViewHolder caches child views using a SparseArray<View> and provides getView(int id) to avoid repeated findViewById() calls.

Key methods: getLayoutId(int viewType) – returns layout resource. convert() – binds data to views.

ItemDecoration

Use addItemDecoration() to add custom dividers. Implement a subclass of RecyclerView.ItemDecoration and override onDraw() and getItemOffsets(). The default Android sample provides DividerItemDecoration as a reference.

Example of obtaining the system divider:

TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.listDivider});
Drawable divider = a.getDrawable();
a.recycle();

In getItemOffsets() set the bottom offset to divider.getIntrinsicHeight() for vertical lists.

LayoutManager

LayoutManager is responsible for laying out child views. Important methods in LinearLayoutManager include: onLayoutChildren() – entry point for layout. fill() – populates the RecyclerView. scrollVerticallyBy() – handles scrolling and calls fill(). canScrollVertically() / canScrollHorizontally() – indicate scroll capability.

The core of onLayoutChildren() repeatedly calls layoutChunk() to fill space.

ItemAnimator

RecyclerView’s default DefaultItemAnimator extends SimpleItemAnimator. Important callbacks include:

animateAppearance() – when a view appears.

animateDisappearance() – when a view disappears.

animatePersistence() – when a view stays but changes.

animateChange() – when notifyItemChanged() is called.

Animations are queued in runPendingAnimations(). Adding custom animators can be simplified with the recyclerview-animators library, which provides ready‑made animators such as FadeInAnimator and ScaleInAnimator.

A common “flash” issue occurs when notifyItemChanged() triggers a change animation that fades the view. Disable it with:

((SimpleItemAnimator) rv.getItemAnimator()).setSupportsChangeAnimations(false);

Extensions

Header and Footer

RecyclerView lacks addHeaderView() and addFooterView(). Implement a wrapper adapter that adds extra view types for header/footer and delegates other types to the original adapter.

Empty View

Create a subclass of RecyclerView (e.g., EmptyRecyclerView) that adds an empty view to the hierarchy and observes adapter data changes to toggle visibility.

Item Click Listener

Since RecyclerView does not provide setOnItemClickListener(), set click listeners inside onBindViewHolder() on holder.itemView.

Drag & Swipe

Use ItemTouchHelper with a custom ItemTouchHelper.Callback to enable drag‑and‑drop and swipe‑to‑delete. Override methods such as getMovementFlags(), onMove(), onSwiped(), onSelectedChanged(), and clearView(). Attach it with:

ItemTouchHelper helper = new ItemTouchHelper(new SimpleItemTouchCallback(adapter, data));
helper.attachToRecyclerView(recyclerView);

For drag initiation from a specific child view, define an OnStartDragListener and call helper.startDrag(holder) from the view’s touch listener.

Recycling Mechanisms

ListView

ListView uses a two‑level cache: mActiveViews for on‑screen views and mScrapViews for off‑screen recycled views. layoutChildren() obtains views from these caches before inflating new ones.

RecyclerView

RecyclerView recycles ViewHolder objects via a four‑level cache: mAttachedScrap – on‑screen holders. mCachedViews – off‑screen holders (default 2). mViewCacheExtensions – optional user‑provided cache. mRecyclerPool – shared pool across RecyclerViews. getViewForPosition() searches these caches in order; if a holder comes from mAttachedScrap or mCachedViews, onBindViewHolder() is skipped, otherwise it is invoked.

Nested Scrolling

Android 5.0 introduced nested scrolling, allowing a child view to pass unhandled scroll events to its parent. RecyclerView implements NestedScrollingChild and works with CoordinatorLayout (which implements NestedScrollingParent) to achieve collapsing toolbars and coordinated scroll effects.

Key attributes:

Toolbar app:layout_collapseMode="pin".

ImageView app:layout_collapseMode="parallax".

RecyclerView app:layout_behavior="@string/appbar_scrolling_view_behavior".

CollapsingToolbarLayout app:layout_scrollFlags="scroll|exitUntilCollapsed".

Review

The article covered many RecyclerView extensions: universal adapters, item click handling, header/footer views, empty view support, drag‑and‑drop, swipe‑to‑delete, and nested scrolling. Most of these features are also provided by popular libraries such as BaseRecyclerViewAdapterHelper.

animationAndroidRecyclerViewadapterLayoutManagerItemDecoration
Tencent TDS Service
Written by

Tencent TDS Service

TDS Service offers client and web front‑end developers and operators an intelligent low‑code platform, cross‑platform development framework, universal release platform, runtime container engine, monitoring and analysis platform, and a security‑privacy compliance suite.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.