Convert Figma logo to code with AI

luizgrp logoSectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers. Each Section can have its state controlled individually.

1,678
371
1,678
0

Top Related Projects

8,512

Epoxy is an Android library for building complex screens in a RecyclerView

"Favor composition over inheritance" for RecyclerView Adapters

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...

Fast and versatile Adapter for RecyclerView which regroups several features into one library to considerably improve the user experience :-)

Flexible multiple types for Android RecyclerView.

3,663

Groupie helps you display and manage complex RecyclerView layouts.

Quick Overview

SectionedRecyclerViewAdapter is an Android library that simplifies the process of creating RecyclerViews with multiple sections. It provides a flexible and easy-to-use API for managing different types of sections within a single RecyclerView, including headers, footers, and expandable groups.

Pros

  • Simplifies the creation of complex RecyclerViews with multiple sections
  • Supports various section types, including headers, footers, and expandable groups
  • Provides a clean and intuitive API for managing sections
  • Offers good performance and efficiency for large datasets

Cons

  • Limited to RecyclerView-based layouts
  • May have a slight learning curve for developers new to sectioned adapters
  • Requires additional setup compared to standard RecyclerView adapters
  • Some advanced customizations might require workarounds

Code Examples

  1. Creating a basic section:
class MySection : StatelessSection(
    SectionParameters.builder()
        .itemResourceId(R.layout.section_item)
        .headerResourceId(R.layout.section_header)
        .build()
) {
    override fun getContentItemsTotal() = 10

    override fun onBindItemViewHolder(holder: ViewHolder, position: Int) {
        // Bind item view
    }

    override fun onBindHeaderViewHolder(holder: ViewHolder) {
        // Bind header view
    }
}
  1. Adding a section to the adapter:
val sectionedAdapter = SectionedRecyclerViewAdapter()
sectionedAdapter.addSection(MySection())
recyclerView.adapter = sectionedAdapter
  1. Creating an expandable section:
class ExpandableSection : ExpandableSection(
    SectionParameters.builder()
        .itemResourceId(R.layout.section_item)
        .headerResourceId(R.layout.section_header)
        .expandableResourceId(R.layout.section_expandable)
        .build()
) {
    override fun getContentItemsTotal() = 5

    override fun onBindItemViewHolder(holder: ViewHolder, position: Int) {
        // Bind item view
    }

    override fun onBindHeaderViewHolder(holder: ViewHolder) {
        // Bind header view
    }

    override fun onBindFooterViewHolder(holder: ViewHolder) {
        // Bind footer view
    }
}

Getting Started

  1. Add the dependency to your app's build.gradle:
implementation 'io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:3.2.0'
  1. Create a section by extending StatelessSection or ExpandableSection:
class MySection : StatelessSection(
    SectionParameters.builder()
        .itemResourceId(R.layout.section_item)
        .headerResourceId(R.layout.section_header)
        .build()
) {
    // Implement required methods
}
  1. Set up the adapter and RecyclerView:
val sectionedAdapter = SectionedRecyclerViewAdapter()
sectionedAdapter.addSection(MySection())
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = sectionedAdapter

Competitor Comparisons

8,512

Epoxy is an Android library for building complex screens in a RecyclerView

Pros of Epoxy

  • More powerful and flexible for complex layouts
  • Built-in support for data binding and view binding
  • Automatic diff calculation for efficient updates

Cons of Epoxy

  • Steeper learning curve due to more complex API
  • Requires more boilerplate code for simple use cases
  • Larger library size and potential performance overhead

Code Comparison

SectionedRecyclerViewAdapter:

class MySection : Section() {
    override fun getContentItemsTotal() = 10
    override fun onBindItemViewHolder(holder: ViewHolder, position: Int) {
        // Bind item view
    }
}

Epoxy:

@EpoxyModelClass
abstract class MyModel : EpoxyModelWithHolder<MyHolder>() {
    @EpoxyAttribute lateinit var title: String
    override fun bind(holder: MyHolder) {
        holder.titleView.text = title
    }
}

Summary

Epoxy offers more advanced features and flexibility for complex RecyclerView layouts, making it suitable for large-scale applications with diverse UI components. It provides built-in support for data binding and view binding, which can simplify development for some projects.

SectionedRecyclerViewAdapter, on the other hand, focuses specifically on creating sectioned layouts with a simpler API. It's more lightweight and easier to implement for basic sectioned lists.

Choose Epoxy for complex, dynamic layouts with diverse item types, and SectionedRecyclerViewAdapter for simpler sectioned lists with a more straightforward implementation.

"Favor composition over inheritance" for RecyclerView Adapters

Pros of AdapterDelegates

  • More flexible and modular approach to handling different view types
  • Easier to add or remove view types without modifying the main adapter
  • Better separation of concerns, with each delegate responsible for its own view type

Cons of AdapterDelegates

  • Slightly more complex setup compared to SectionedRecyclerViewAdapter
  • May require more boilerplate code for simple use cases
  • Doesn't provide built-in support for section headers and footers

Code Comparison

SectionedRecyclerViewAdapter:

public class MyAdapter extends SectionedRecyclerViewAdapter {
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int section, int relativePosition, int absolutePosition) {
        // Bind data to views
    }
}

AdapterDelegates:

public class MyAdapter extends ListDelegationAdapter<List<Item>> {
    public MyAdapter() {
        delegatesManager.addDelegate(new TypeAAdapterDelegate());
        delegatesManager.addDelegate(new TypeBAdapterDelegate());
    }
}

Both libraries offer solutions for handling multiple view types in RecyclerView, but they approach the problem differently. SectionedRecyclerViewAdapter focuses on creating sections with headers and footers, while AdapterDelegates provides a more modular approach for handling different view types. The choice between them depends on the specific requirements of your project and personal preference.

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...

Pros of FastAdapter

  • More comprehensive feature set, including drag & drop, swipe-to-dismiss, and expandable items
  • Extensive documentation and sample apps for easier implementation
  • Active development with frequent updates and community support

Cons of FastAdapter

  • Steeper learning curve due to its extensive feature set
  • Potentially higher memory footprint for simpler list implementations
  • May require more setup code for basic use cases

Code Comparison

SectionedRecyclerViewAdapter:

SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
sectionAdapter.addSection(new MySection());
recyclerView.setAdapter(sectionAdapter);

FastAdapter:

FastAdapter<Item> fastAdapter = FastAdapter.with(items);
recyclerView.setAdapter(fastAdapter);

Summary

FastAdapter offers a more feature-rich solution with extensive customization options, making it suitable for complex list implementations. However, this comes at the cost of a steeper learning curve and potentially more setup code. SectionedRecyclerViewAdapter, on the other hand, provides a simpler approach focused specifically on sectioned lists, which may be preferable for straightforward use cases. The choice between the two depends on the specific requirements of your project and the level of complexity you need in your RecyclerView implementation.

Fast and versatile Adapter for RecyclerView which regroups several features into one library to considerably improve the user experience :-)

Pros of FlexibleAdapter

  • More feature-rich, offering advanced functionalities like expandable items, multi-level sections, and drag & drop
  • Provides built-in animations and customizable item decorations
  • Offers comprehensive documentation and sample apps for easier implementation

Cons of FlexibleAdapter

  • Steeper learning curve due to its extensive feature set
  • Larger library size, which may impact app size and performance
  • More complex setup process compared to SectionedRecyclerViewAdapter

Code Comparison

FlexibleAdapter:

FlexibleAdapter<IFlexible> adapter = new FlexibleAdapter<>(items);
recyclerView.setAdapter(adapter);
adapter.addSection(new HeaderItem("Section Title"));
adapter.addItem(new SimpleItem("Item 1"));

SectionedRecyclerViewAdapter:

SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
sectionAdapter.addSection(new MySection("Section Title", itemList));
recyclerView.setAdapter(sectionAdapter);

Both libraries provide solutions for creating sectioned RecyclerViews in Android, but FlexibleAdapter offers more advanced features at the cost of increased complexity. SectionedRecyclerViewAdapter is simpler and more lightweight, making it suitable for basic sectioned layouts. The choice between the two depends on the specific requirements of your project and the level of customization needed.

Flexible multiple types for Android RecyclerView.

Pros of MultiType

  • More flexible and extensible, allowing for easier implementation of complex list structures
  • Simpler API with less boilerplate code required
  • Better support for Kotlin and coroutines

Cons of MultiType

  • Lacks built-in support for section headers and footers
  • May require more manual work for implementing section-specific features
  • Less focused on sectioned lists, which might be a drawback for specific use cases

Code Comparison

SectionedRecyclerViewAdapter:

Section section = new Section(headerResourceId, footerResourceId);
section.addItem(new Item("Item 1"));
section.addItem(new Item("Item 2"));
sectionAdapter.addSection(section);

MultiType:

val items = listOf(
    Header("Section 1"),
    Item("Item 1"),
    Item("Item 2"),
    Footer("End of Section 1")
)
adapter.items = items

Both libraries provide solutions for creating complex RecyclerView layouts, but they approach the problem differently. SectionedRecyclerViewAdapter focuses specifically on sectioned lists, offering built-in support for headers and footers. MultiType, on the other hand, provides a more general-purpose solution that can be adapted to various list structures, including sectioned lists, but may require more custom implementation for section-specific features.

3,663

Groupie helps you display and manage complex RecyclerView layouts.

Pros of groupie

  • More flexible item types, allowing for easier composition of complex layouts
  • Built-in support for expandable groups and sections
  • Simpler API for adding and updating items in the adapter

Cons of groupie

  • Steeper learning curve due to more abstract concepts
  • Potentially higher memory usage for complex layouts
  • Less control over low-level RecyclerView operations

Code Comparison

SectionedRecyclerViewAdapter:

class MySection : Section() {
    override fun getContentItemsTotal(): Int = 10
    override fun onBindItemViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        // Bind item view
    }
}

groupie:

class MyItem : Item<ViewHolder>() {
    override fun bind(viewHolder: ViewHolder, position: Int) {
        // Bind item view
    }
}

Summary

SectionedRecyclerViewAdapter focuses on creating sectioned layouts with a straightforward API, while groupie offers more flexibility for complex layouts and item types. SectionedRecyclerViewAdapter may be easier to learn for developers familiar with RecyclerView, but groupie provides more powerful features for advanced use cases. The choice between the two depends on the specific requirements of your project and your team's familiarity with RecyclerView concepts.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

:warning: Archived: this repository is no longer going to be maintained.

SectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers.

Build Status codecov Android Arsenal

Linear Grid

In addition, each Section can have its state(Loading/Loaded/Failed/Empty) controlled individually.

Loading Loaded


Gradle Dependency

Step 1: Add jitpack repository to the top-level build.gradle file:
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
Step 2: Add this library to the module-level build.gradle file:
dependencies {
	...
	implementation 'com.github.luizgrp:SectionedRecyclerViewAdapter:v3.2.0'
}

Guide to upgrade to version 3.x here.

Latest version without AndroidX: 1.2.0.

Basic usage

1) Create a custom Section class:
class MySection extends Section {
    List<String> itemList = Arrays.asList("Item1", "Item2", "Item3");

    public MySection() {
        // call constructor with layout resources for this Section header and items
        super(SectionParameters.builder()
                .itemResourceId(R.layout.section_item)
                .headerResourceId(R.layout.section_header)
                .build());
    }

    @Override
    public int getContentItemsTotal() {
        return itemList.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(itemList.get(position));
    }
    
    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        // return an empty instance of ViewHolder for the headers of this section
        return new EmptyViewHolder(view);
    }
}
2) Create a custom ViewHolder for the section items:
class MyItemViewHolder extends RecyclerView.ViewHolder {
    private final TextView tvItem;

    public MyItemViewHolder(View itemView) {
        super(itemView);

        tvItem = (TextView) itemView.findViewById(R.id.tvItem);
    }
}
3) Set up your RecyclerView with the SectionedRecyclerViewAdapter:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Add your Sections
sectionAdapter.addSection(new MySection());

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);

Demo app

You can find a demo app here with many examples on how to implement:

Demo

Apps on Google Play using this library

License

The MIT License (MIT)

Copyright (c) 2016 Gustavo Pagani

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.