Skip to content

Android RecyclerView LayoutManager — Complete Guide

DodaTech Updated 2026-06-24 1 min read

In this tutorial, you'll learn about Android RecyclerView LayoutManager. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Your RecyclerView is inside a NestedScrollView and items are invisible, or the grid has uneven spacing, or you're using wrap_content incorrectly.

Wrong Approach ❌

<ScrollView>
    <LinearLayout>
        <!-- RecyclerView inside ScrollView without nestedScrollingEnabled -->
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>
recyclerView.layoutManager = LinearLayoutManager(this)
// No optimisations — full layout pass on every scroll

Output: RecyclerView collapses to 0dp height. Laggy scrolling.

Right Approach ✅

<androidx.coordinatorlayout.widget.CoordinatorLayout>
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="false" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
val layoutManager = LinearLayoutManager(this).apply {
    isItemPrefetchEnabled = true // Performance boost
}
recyclerView.layoutManager = layoutManager
recyclerView.setHasFixedSize(true) // If height is fixed

Output: Properly measured RecyclerView with smooth scrolling.

Prevention

  • Never nest a RecyclerView inside a ScrollView or NestedScrollView.
  • Use Android:nestedScrollingEnabled="false" for nested RecyclerViews.
  • Call setHasFixedSize(true) when the content size doesn't change.
  • Use GridLayoutManager with spanSizeLookup for uneven grids.

Common Mistakes with recyclerview layout

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why is my RecyclerView not scrolling?

You likely have a touch-intercepting parent (like ScrollView or ViewPager). Either disable nested scrolling on the RecyclerView or handle touch conflicts with requestDisallowInterceptTouchEvent.

### What is the best LayoutManager for a horizontal list?

Use LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false). For a horizontal grid, combine with GridLayoutManager orientation support.

### How do I add spacers between grid items?

Use ItemDecoration with getItemOffsets(). Override onDraw() if you need visual dividers. Never add margins to item layouts — they interfere with span calculations.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro