An adapter to create Android RecyclerViews with sections, providing headers and footers.

Overview

This library is no longer maintained ⚠️

SectionedRecyclerView Download Android Arsenal

An adapter to create Android RecyclerViews with sections, providing headers and footers.

Usage

In order to use this library, you need to extend SectionedRecyclerView<H, VH, F> where:

  • H is a class extending RecyclerView.ViewHolder to hold the view for section headers.
  • VH is a class extending RecyclerView.ViewHolder to hold the view for the regular items in the view.
  • F is a class extending RecyclerView.ViewHolder to hold the view for section footers.

According to the sample published in this repository:

    1. Create a class extending SectionedRecyclerView:
public class CountSectionAdapter extends SectionedRecyclerViewAdapter<CountHeaderViewHolder,
        CountItemViewHolder,
        CountFooterViewHolder>
    1. Implement the corresponding methods:
@Override
protected int getItemCountForSection(int section) {
    return section + 1;
}

@Override
protected int getSectionCount() {
    return 5;
}

@Override
protected boolean hasFooterInSection(int section) {
    return true;
}

protected LayoutInflater getLayoutInflater(){
    return LayoutInflater.from(context);
}

@Override
protected CountHeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
    View view = getLayoutInflater().inflate(R.layout.view_count_header, parent, false);
    return new CountHeaderViewHolder(view);
}

@Override
protected CountFooterViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
    View view = getLayoutInflater().inflate(R.layout.view_count_footer, parent, false);
    return new CountFooterViewHolder(view);
}

@Override
protected CountItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
    View view = getLayoutInflater().inflate(R.layout.view_count_item, parent, false);
    return new CountItemViewHolder(view);
}

@Override
protected void onBindSectionHeaderViewHolder(CountHeaderViewHolder holder, int section) {
    holder.render("Section " + (section + 1));
}

@Override
protected void onBindSectionFooterViewHolder(CountFooterViewHolder holder, int section) {
    holder.render("Footer " + (section + 1));
}

protected int[] colors = new int[]{0xfff44336, 0xff2196f3, 0xff009688, 0xff8bc34a, 0xffff9800};
@Override
protected void onBindItemViewHolder(CountItemViewHolder holder, int section, int position) {
    holder.render(String.valueOf(position + 1), colors[section]);
}
    1. If you use a GridLayoutManager, you need to set it a SectionedSpanSizeLookup to make sure that headers and footers span the whole width of the RecyclerView:
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
SectionedSpanSizeLookup lookup = new SectionedSpanSizeLookup(adapter, layoutManager);
layoutManager.setSpanSizeLookup(lookup);
recycler.setLayoutManager(layoutManager);
    1. Your result will look like this:

SectionedRecyclerView screenshot

Even simpler

Most times you will need a simpler version of this adapter, where there are no footers and your headers will only be a title. For those cases, you have SimpleSectionedAdapter<VH>, where VH is a class extending ViewHolder to hold the view of the regular items in your RecyclerView.

In this case, you will have to implement the following methods:

@Override
protected String getSectionHeaderTitle(int section) {
    return section == 0 ? "Today" : "Tomorrow";
}

@Override
protected int getSectionCount() {
    return 2;
}

@Override
protected int getItemCountForSection(int section) {
    return 3;
}

@Override
protected AgendaItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.view_agenda_item, parent, false);
    return new AgendaItemViewHolder(view);
}

protected String[][] agenda = {{"Meeting", "Phone call", "Interview"},
            {"Basket match", "Grocery shopping", "Taking a nap"}};

@Override
protected void onBindItemViewHolder(AgendaItemViewHolder holder, int section, int position) {
    holder.render(agenda[section][position]);
}

Your result will look like this:

SimpleSectionedAdapter screenshot

Get it!

SectionedRecyclerView is available through JCenter. To be able to use this library in your project, add the following dependency to your build.gradle file:

dependencies{
	compile 'com.truizlop.sectionedrecyclerview:library:1.2.0'
}

License

Copyright 2015 Tomás Ruiz-López

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Recycleview returns wrong index on click

    Recycleview returns wrong index on click

    Hi,

    I am using common available method to get recycleview items. I have noticed 2 things here.

    1. Why header is clickable ?
    2. I think due to header it calculates recycleview's first item as 2nd item.

    What is solution ?

    opened by homedevs 6
  • Is there a way to add extra itemTypes

    Is there a way to add extra itemTypes

    I see that you are using itemTypes for the footers and headers. I'd like to have a section using other viewHolder type. I haven't digged enough to be sure that another type is possible.

    public int getItemViewType(int position);

    Is there a way to get section index inside this method?

    opened by Qoderapps 3
  • Setups indices on attached instead of constructor

    Setups indices on attached instead of constructor

    Removes the call to setupIndices() indirectly invoked by notifyDataSetChanged() in the constructor. Instead, the initial setup of indices get's done in onAttachedToRecyclerView(), when the adapter has been created and initialized probably.

    This fixes the problem with calling child's getSectionCount() when it depends on non-null fields initialization at construction time, since the parent constructor get's called before the child's.

    Fixes #3 and #4

    Also, I removed super.onChanged(); in SectionDataObserver since the parent method is empty.

    onAttachedToRecyclerView() is the best place for the initialization that I could find, can't assure it is the best one. But I don't see a reason not to use it.

    Hope it helps!

    opened by Sloy 3
  • Does it work with RecyclerView.SortedList?

    Does it work with RecyclerView.SortedList?

    I was having some issues with displaying the list items where all the headers had same title. rec Adapter was returning correct title while calling getSectionHeaderTitle from 'onBindItemViewHolder'

    opened by subediaayush 2
  • Remove android:allowBackup=

    Remove android:allowBackup="true"

    Allow apps using the library to select their own backup option without needing to override it. Libraries should't use allowBackup in theeir manifest. This provokes a build error in recent versions of the Gradle plugin when the application sets the value to false.

    Temporary workaround is to add tools:replace="android:allowBackup" to the application tag in the app's manifest.

    I'm creating the pull request against master because I don't see there is any other branch for development.

    opened by Sloy 2
  • Nullpointer when set section count from list size

    Nullpointer when set section count from list size

    Hi im trying set section count using list size, but always give me null pointer, i have been trace my code, and im sure my arraylist not null, here are my adapter

    _Adapter_ https://gist.github.com/anonymous/fd7d7b0a5ee22532f626

    _my MainActivity_ https://gist.github.com/anonymous/6767cbd484bbd4145487

    _my MainPresenter_ https://gist.github.com/anonymous/2f6c4e4bde3cb4e16dab

    opened by pratamawijaya 2
  • RecyclerView height problem with support library 23.2.0 and up

    RecyclerView height problem with support library 23.2.0 and up

    It's fine with 23.1.1 but it looks like the following with 23.2.0 and up: (I set the background of my grid item has blue) screen shot 2016-08-01 at 9 47 47 pm I want to call recyclerView.setHasFixedSize(true) to optimise performance.

    The height is fine when I use recyclerView.setHasFixedSize(false) though

    opened by ericntd 1
  • Weird height problem with ImageView

    Weird height problem with ImageView

    screen shot 2016-07-29 at 10 24 32 am

    My grid_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ListItemProduct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_dark"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingLeft="@dimen/margin_sm"
        android:paddingRight="@dimen/margin_sm"
        android:paddingTop="0dp"
        android:layout_marginTop="0dp">
    
        <ImageView
            android:id="@+id/imgProduct"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:layout_marginTop="0dp"
            android:scaleType="centerInside"
            android:src="@drawable/ic_home_black_24dp"
            android:background="@android:color/holo_blue_bright"/>
    
        <!--<ImageView-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_marginTop="-24dp"-->
            <!--android:id="@+id/imageView4"-->
            <!--android:src="@drawable/ic_favorite_border_black_24dp"-->
            <!--android:layout_gravity="right"/>-->
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textSpecial"/>
    
        <TextView
            android:id="@+id/textBrand"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_weight="1"
            android:singleLine="true"
            android:maxLines="1"
            android:textSize="@dimen/font_size_sm"
            android:textStyle="bold" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textName"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
    
            <TextView
                android:id="@+id/textPrice"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_weight="1"
                android:singleLine="true"
                android:maxLines="1"
                android:textSize="@dimen/font_size_sm"
                android:textStyle="bold" />
    
            <TextView
                android:id="@+id/textSalePrice"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:textStyle="bold"
                android:layout_weight="1"
                android:gravity="end"
                android:textColor="@color/colorAccent"
                android:textSize="@dimen/font_size_sm" />
    
        </LinearLayout>
    
        <android.support.v7.widget.AppCompatRatingBar
            android:id="@+id/ratingBar2"
            style="?android:attr/ratingBarStyleSmall"
            android:progressTint="@color/colorAccent"
            android:backgroundTint="@color/colorAccent"
            android:secondaryProgressTint="@color/colorAccent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:isIndicator="true"
            android:numStars="5"
            android:stepSize="0.1" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textVariants"/>
    
    </LinearLayout>
    

    I printed out the image views getHeight() and getMeasuredHeight() and got 0 for both. When I changed the span count for GridLayoutManager to 1, all images have correct height. When I changed the span count to 3, only the last of the 1st row, first of 2nd row, last of 3rd row and so on have the correct image height.

    opened by ericntd 1
  • No layout manager attached!!!

    No layout manager attached!!!

    Hi, I have a problem to visualize data when I run your example 'AgendaSimpleSectionAdapter', I get this error: E/RecyclerView﹕ No layout manager attached; skipping layout

    opened by javierpe 1
  • I used your library and the following problem

    I used your library and the following problem

    Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

    opened by if-time 0
  • Is there a way to give background to individual sections?

    Is there a way to give background to individual sections?

    In my app i have 5 sections, I'm able to set background to individual items, but I need to give background to individual sections. Suppose I have a section Videos , I need to add background for the whole section and different background for whole Audio section how can I achieve this ?

    opened by sujeshshetty 0
Owner
Tomás Ruiz-López
iOS/Swift Software Engineer at GoodNotes. Creator of @bow_swift.
Tomás Ruiz-López
Beautify your RecyclerViews with a great parallax effect !

BeautifulParallax Beautify your RecyclerViews with a great parallax effect ! Without Carpaccio public class YOURAdapter extends RecyclerView.Adapter<Y

Florent CHAMPIGNY 413 Nov 2, 2022
A layout manager for the RecyclerView with interchangeable linear, grid, and staggered displays of views, all with configurable section headers including the sticky variety as specified in the material design docs.

SuperSLiM This is the version 5 development branch. Project Plan Support me on Patreon Blog What is Version 5 Version 5 is the current development bra

Tonic Artos 2.1k Jan 2, 2023
[UNMAINTAINED] Sticky Headers decorator for Android's RecyclerView

This project is no longer being maintained sticky-headers-recyclerview This decorator allows you to easily create section headers for RecyclerViews us

timehop 3.7k Jan 8, 2023
Android library providing simple way to control divider items (ItemDecoration) of RecyclerView

RecyclerView-FlexibleDivider Android library providing simple way to control divider items of RecyclerView Release Note [Release Note] (https://github

Yoshihito Ikeda 2.4k Dec 18, 2022
A Common RecyclerView.Adapter implementation which supports all kind of items and has useful data operating APIs such as remove,add,etc.

##PowerfulRecyclerViewAdapter A Common RecyclerView.Adapter implementation which supports any kind of items and has useful data operating APIs such as

null 313 Nov 12, 2022
Elegant design and convenient to use RecyclerView adapter library based on Kotlin DSL.

xAdapter: Kotlin DSL 风格的 Adapter 封装 1、简介 该项目是 KotlinDSL 风格的 Adapter 框架封装,用来简化 Adapter 调用,思想是采用工厂和构建者方式获取 Adapter 避免代码中定义大量的 Adapter 类。该项目在 BRVAH 的 Ada

ShouHeng 17 Oct 9, 2022
Add RecyclerView, use Adapter class and ViewHolder to display data.

فكرة المشروع في هذا المشروع سنقوم بعرض قائمة من البيانات للطلاب على واجهة تطبيق Android بإستخدام: مفهوم RecyclerView مفهوم Adapter مفهوم ViewModel محت

Shaima Alghamdi 3 Nov 18, 2021
Shimo is an adapter for Moshi which randomizes the order of keys when serializing and deserializing

Shimo Shimo is a JsonAdapter.Factory for Moshi which randomizes the order of keys when serializing objects to JSON and when deserializing objects from

Jake Wharton 179 Dec 19, 2022
Android library defining adapter classes of RecyclerView to manage multiple view types

RecyclerView-MultipleViewTypeAdapter RecyclerView adapter classes for managing multiple view types Release Note [Release Note] (https://github.com/yqr

Yoshihito Ikeda 414 Nov 21, 2022
Don't write a RecyclerView adapter again. Not even a ViewHolder!

LastAdapter Don't write a RecyclerView adapter again. Not even a ViewHolder! Based on Android Data Binding Written in Kotlin No need to write the adap

Miguel Ángel Moreno 781 Dec 19, 2022
kotlin dsl for kids to simplify RecyclerView.Adapter logic

KidAdapter RecyclerView adapter for kids. A kotlin dsl mechanism to simplify and reduce boilerplate logic of a RecyclerView.Adapter. With KidAdapter y

Eugeniu Tufar 56 Nov 27, 2022
A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps

Infinite Recycler View A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps. This library offers you a custom adapt

IB Sikiru 26 Dec 10, 2019
Reproducible sample with Fix for Memory Leak in RecyclerView Adapter

Memory Leak RecyclerView Adapter Reproducible Sample with Fix Video Instructions: https://www.youtube.com/c/awesomedevnotes Code Only the relevant and

Awesome Dev Notes | Android Dev Notes YouTube 7 Jun 7, 2022
RecyclerView With No Adapter | Available For Jetpack Compose

About This Project Available on Google Dev Library Click Here RecyclerView No Adapter (Adapter Has Been Handled) RecyclerView No Adapter Using ViewBin

Faisal Amir 142 Oct 18, 2022
Easy RecyclerView Adapter

GenericAdapter Easy RecyclerView Adapter Getting started build.gradle allprojects { repositories { // ... maven { url 'https://jit

JaredDoge 4 Dec 3, 2021
Yet another adapter delegate library.

Yet another adapter delegate library. repositories { ... maven { url 'https://jitpack.io' } } ... dependencies { implementation("com.git

Mike 10 Dec 26, 2022
Just another one easy-to-use adapter for RecyclerView :rocket:

Elementary RecyclerView Adapter Another one easy-to-use adapter for RecyclerView ?? Features: DSL-like methods for building adapters similar to Jetpac

Roman Andrushchenko 29 Jan 6, 2023
[] Super fast and easy way to create header for Android RecyclerView

DEPRECATED I created this library back in the day when I thought RecyclerView was all new and difficult. Writing an adapter that could inflate multipl

Bartek Lipinski 1.3k Dec 28, 2022