Dividers is a simple Android library to create easy separators for your RecyclerViews

Overview

Karumi logo Dividers Build Status Maven Central Android Arsenal

Dividers is an Android library to easily create separators for your RecyclerViews. It supports a wide range of dividers from simple ones, that apply to all your items equally, to a system of selectors that apply different styles to each item.

Screenshots

Demo screenshot

Usage

The easiest way to start using Dividers is to create a DividerItemDecoration with a layer and provide it to your RecyclerView as follows:

// Create a drawable for your divider
Drawable exampleDrawable = getResources().getDrawable(R.drawable.example_drawable);

// Create a DividerItemDecoration instance with a single layer and add it to your recycler view
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(new Layer(DividerBuilder.get().with(exampleDrawable).build()));
recyclerView.addItemDecoration(itemDecoration);

If you want to use all the features of Dividers follow these steps:

  • Create a collection of instances of Layer with the help of LayersBuilder to define the drawables you want to apply. Each Layer is composed of:
  • An implementation of the Selector interface, defining which items are going to be affected by the layer. You can use one of the multiple implementations that come in the library or create your own.
public class HighRatingMovieSelector implements Selector {

  private final List<Movie> movies;
  private final int maxHighRating;

  public HighRatingMovieSelector(List<Movie> movies, int maxHighRating) {
    this.movies = movies;
    this.maxHighRating = maxHighRating;
  }

  @Override public boolean isPositionSelected(Position position) {
    return movies.get(position.getAbsoluteIndex()).getRating() >= maxHighRating;
  }

  @Override public EnumSet<Direction> getDirectionsByPosition(Position position) {
    return EnumSet.allOf(Direction.class);
  }
}
  • An instance of Divider, containing the drawable that is going to be used to render the separator. Create them using the DividerBuilder class.
Drawable drawable = getResources().getDrawable(R.drawable.example_divider);
Divider divider = DividerBuilder.from(highlightedDrawable).build()
  • Finally, create a collection of Layer instances with the help of LayersBuilder
Collection<Layer> layers = LayersBuilder.with(new Layer(new HighRatingMovieSelector(), divider)).build();
  • Create a new instance of DividerItemDecoration with your newly created layers.
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(layers);
  • Add your DividerItemDecoration to your RecyclerView using the addItemDecoration method.
recyclerView.addItemDecoration(itemDecoration);

Selectors

Selectors give you the possibility to apply different configurations to a fixed set of items of your view. There are multiple implementations to apply the same separator to every item in a single row (AllItemsInRowSelector) or column (AllItemsInColumnSelector).

For example, to show the divider in all the elements except the header you can use the following snippet:

Drawable drawable = getResources().getDrawable(R.drawable.example_drawable);

// Create a layer that applies to all the layers. It's the same as writing :
// Layer allItemsLayer = new Layer(new AllItemsSelector(), DividerBuilder.from(drawable).build());
Layer allItemsLayer = new Layer(DividerBuilder.from(drawable).build());

// Create a layer that applies to all the items in the header with an empty drawable to avoid displaying anything
Layer headerLayer = new Layer(new HeaderSelector(), DividerBuilder.fromEmpty().build());

// Add your layers to the DividerItemDecoration class and provide it to your recycler view!
recyclerView.addItemDecoration(new DividerItemDecoration(LayersBuilder.from(allItemsLayer, headerLayer).build()));

You can also create your own Selector to apply dividers to all items but the ones in the header:

public class NotInHeaderSelector implements Selector {
  @Override public boolean isPositionSelected(Position position) {
    return !position.isFirstRow();
  }

  @Override public EnumSet<Direction> getDirectionsByPosition(Position position) {
    return EnumSet.allOf(Direction.class);
  }
}

As you have seen, selectors also let you apply a divider to some sides of every item depending on things such as the position. In this way you can create selectors to display items as a whole, you can take a look at some examples like RowGroupSelector:

public class RowGroupSelector extends AllItemsInRowSelector {

  protected final int row;

  public RowGroupSelector(int row) {
    this.row = row;
  }

  @Override public boolean isPositionSelected(Position position) {
    return position.getRow() == row;
  }

  @Override public EnumSet<Direction> getDirectionsByPosition(Position position) {
    // Every component of the row will need to draw, at least, SOUTH_WEST, WEST, NORTH_WEST, 
    // NORTH_EAST, EAST and SOUTH_EAST directions
    EnumSet<Direction> directions = EnumSet.complementOf(
        EnumSet.of(Direction.WEST, Direction.EAST));

    // If we are the element of the left side we also need to draw the west direction
    if (position.isFirstColumn()) {
      directions.add(Direction.WEST);
    }
    // If we are the element of the right side we also need to draw the east direction
    if (position.isLastColumn()) {
      directions.add(Direction.EAST);
    }

    return directions;
  }
}

Dividers

Dividers are the smallest components that can be rendered and represent all the separators for a single cell. Internally, dividers are represented as a grid of 3x3 elements (ignoring the center). Each side can be referenced with a value from the enum Direction. This can prove useful when defining complex dividers such as the ones used in headers or footers.

Add it to your project

Add Dividers dependency to your build.gradle file

dependencies{
    compile 'com.karumi:dividers:1.0.1'
}

or to your pom.xml if you are using Maven instead of Gradle

<dependency>
    <groupId>com.karumi</groupId>
    <artifactId>dividers</artifactId>
    <version>1.0.1</version>
    <type>aar</type>
</dependency>

Do you want to contribute?

Feel free to add any useful feature to the library, we will be glad to improve it :)

Libraries used in this project

  • [JUnit] 2
  • [Picasso] 3
  • [Butterknife] 4

External resources used in this project

  • IMDB.com ©

License

Copyright 2015 Karumi

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
  • Divider.java changes the bounds of a Drawable that has not been mutate()d.

    Divider.java changes the bounds of a Drawable that has not been mutate()d.

    From the docs:

    By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

    Calling Drawable#mutate() before manipulating the Drawable could be a solution.

    bug 
    opened by emmano 3
  • Add offset support

    Add offset support

    This branch contains several improvements:

    • Add an offset support for easily handle headers in RecyclerViews with GridLayouts.
    • Adds some useful shortcuts for selecting Directions.
    • Simplify some internal logic in PositionAdapter
    • Add a new OffsetSelector to apply dividers to items with an adapter index equal or higher than a specified position.
    • Adds an eraser method to avoid painting a certain direction in DividerBuilder (instead of declaring the whole Divider as empty and declaring which sides should paint you can now do it the other way around).
    enhancement 
    opened by Serchinastico 1
  • Add Maven Central badge

    Add Maven Central badge

    Using this open source project -> https://github.com/jirutka/maven-badges We can add a little badge next to the two we already have to show the latest version of this library.

    enhancement 
    opened by pedrovgs 1
  • Simplify public API

    Simplify public API

    Creating the most simple divider implies creating tons of intermediary classes. We should make the gap for starters as small as possible and let the user decide when to learn more complex concepts.

    I'll think about a redesign of the API and write it down in this ticket so that everyone can participate and discuss the matter.

    enhancement 
    opened by Serchinastico 0
  • Add composable selectors

    Add composable selectors

    Add a few composable selectors like:

    • AndSelector that performs the drawing only if two or more provided selectors affects that cell. It will draw the intersection of all the sides provided by all of his child selectors.
    • OrSelector basically the same as AndSelector but with the OR boolean operator.
    enhancement 
    opened by Serchinastico 0
  • Add drawable strategy

    Add drawable strategy

    Instead of the plain simple top-to-down & left-to-right approach we can make the drawing strategy configurable and add modes as every cell drawing half of its dividers or start drawing from the center to the limits.

    enhancement 
    opened by Serchinastico 0
Releases(1.0.3)
Owner
Karumi
Karumi, the Rock Solid Code studio
Karumi
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
[] 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
Simple lib to create a endless recycler view scroll

Endless RecyclerView A simple lib to create an infinite list in a RecyclerView. When you reach the end of the list, a callback is triggered, where you

Jaison Klemer 4 Sep 2, 2022
Pagination-RecyclerView - Simple and easy way to Paginating a RecyclerView

Pagination-RecyclerView Simple and easy way to Paginating a RecyclerView Android

Rakshit Nawani 0 Jan 3, 2022
A customizable and easy-to-use Timeline View library for Android

TimelineView A customizable and easy-to-use Timeline View library for Android Can be used as a standalone view or as a RecyclerView decorator Setup 1.

Riccardo Lattarulo 189 Dec 10, 2022
Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Jack and phantom 504 Dec 25, 2022
Handy library to integrate pagination, which allow no data layout, refresh layout, recycler view in one view and easy way to bind pagination in app.

Pagination View Handy library to integrate pagination, which allow no data layout, refresh layout, recycler view in one view and easy way to bind pagi

DhiWise 4 Dec 18, 2021
A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView.

RecyclerViewSwipeDismiss A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView. Preview How to use Add these lines to yo

xcodebuild 431 Nov 23, 2022
A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView.

RecyclerViewSwipeDismiss A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView. Preview How to use Add these lines to yo

xcodebuild 431 Nov 23, 2022
Easy RecyclerView Adapter

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

JaredDoge 4 Dec 3, 2021
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
Organize your images in beautiful collage with this library!

CollageImageView This app is an example. how to create collages with RecyclerView. See an example, how it's working: device-2021-04-24-015545.mp4 Inst

Sergey Grishin 16 Oct 14, 2022
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
Affirmations-App - Your Daily Affirmations Android Application

Affirmations-App Your Daily Affirmations Android Application Concepts Used Mater

Atul Sharma 0 Feb 2, 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
RecyclerView with DiffUtil is a way to improve the performance of your app

RecylerViewSamples RecyclerView with DiffUtil is a way to improve the performanc

Chhote Lal Pal 0 Dec 20, 2021
SleepTracker - A sample application to track your sleep

Room - SleepQualityTracker app This is the toy app for Lesson 6 of the Android A

null 0 Feb 2, 2022
[] RecyclerView made simple

TwoWayView RecyclerView made simple. Features A LayoutManager base class that greatly simplifies the development of custom layouts for RecyclerView A

Lucas Rocha 5.3k Dec 30, 2022
Simple plug and play custom RecyclerView

InfiniteScrollRecyclerView Pros: Simple plug and play custom RecyclerView. Easy to use Built on top of RecyclerView, hence it is performant as it work

Frontier 61 Dec 28, 2022