• This repository has been archived on 23/Apr/2019
  • Stars
    star
    806
  • Rank 54,645 (Top 2 %)
  • Language
    Java
  • Created almost 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

More Repositories

1

FABRevealLayout

A layout to transition between two views using a Floating Action Button as shown in many Material Design concepts
Java
898
star
2

FuzzyFind

A Swift package to perform fuzzy search queries on strings
Swift
132
star
3

IntroToBow

A Swift Playground to get started using Bow
Swift
37
star
4

CategoryTheoryForProgrammers

Notes from Category Theory for Programmers lessons by Bartosz Milewski
28
star
5

ConwaysGameOfLife

An implementation of Conway's Game of Life in Swift, using Bow and SwiftUI
Swift
17
star
6

GOFToLambda

GOF Patterns in Functional light
Swift
14
star
7

ImperativeToFunctional

An example of using Bow to transform imperative code into functional
Swift
13
star
8

TicTacToeArch

Tic Tac Toe game using SwiftUI and Bow Arch
Swift
11
star
9

CopyGenerator

Xcode Source Extension to generate initializers and copy methods
Swift
9
star
10

BowHangman

A purely functional implementation of Hangman using Bow for Swift
Swift
5
star
11

LearningSwiftByExample

A little experiment to show how you can write a Swift tutorial, written in Swift, that reads in plain English and executes as a suite of tests.
Swift
5
star
12

HomeScreenMonoid

Code examples for the blog post series "Your iOS Home Screen is a Monoid"
Swift
4
star
13

TitanicTensorFlow

A sample project using Swift for TensorFlow to predict survival on the Titanic catastrophe
Swift
3
star
14

DemystifyingFP

Playground with source code for an episode of Swift Community
Swift
1
star
15

WorkingWithOptics

Sample playground demonstrating the creation and usage of Bow Optics
Swift
1
star
16

DataValidationBow

Slides for error handling and data validation with Bow
JavaScript
1
star