• This repository has been archived on 10/Jan/2020
  • Stars
    star
    483
  • Rank 91,050 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 6 years ago

Reviews

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

Repository Details

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

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.

More Repositories

1

Dexter

Android library that simplifies the process of requesting permissions at runtime.
Java
5,233
star
2

Rosie

Rosie is an Android framework to create applications following the principles of Clean Architecture.
Java
1,824
star
3

ExpandableSelector

ExpandableSelector is an Android library created to show a list of Button/ImageButton widgets inside a animated container which can be collapsed or expanded.
Java
693
star
4

KataSuperHeroesAndroid

Super Heroes Kata for Android Developers. The main goal is to practice UI Testing.
Java
683
star
5

HeaderRecyclerView

HeaderRecyclerView is an Android library created to be able to use RecyclerView.Adapter with a header in a easy way. To use this library create your RecyclerView.Adapter classes extending from HeaderRecyclerViewAdapter.
Java
501
star
6

AndroidAudit

Your Android app as a crime scene!!!
468
star
7

BothamUI

Model View Presenter Framework written in Swift.
Swift
347
star
8

KataScreenshotAndroid

Screenshot Kata for Android Developers. The main goal is to practice UI Testing.
Java
265
star
9

KataContactsJava

KataContacts written in Java. The main goal is to practice Clean Architecture Development.
Java
111
star
10

project-quality-assurance

Define a general guidance for tech projects quality assurance at Karumi.
102
star
11

KataSuperHeroesKotlin

Super Heroes Kata for Android Developers in Kotlin. The main goal is to practice UI Testing.
Kotlin
86
star
12

KataScreenshotKotlin

Screenshot Kata for Android Developers with Kotlin. The main goal is to practice UI Screenshot Testing.
Kotlin
76
star
13

KataSuperHeroesIOS

Super heroes kata for iOS Developers. The main goal is to practice UI Testing.
Swift
69
star
14

learnyougit

A self-guided workshop to learn the basics and some of the internals of Git
JavaScript
67
star
15

MarvelApiClientAndroid

Api client for Marvel Api
Java
66
star
16

MaxibonKataJava

Maxibon kata for Java Developers. The main goal is to practice property based testing.
Java
65
star
17

KataTODOApiClientKotlin

TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer
Kotlin
60
star
18

KataContactsSwift

KataContacts written in Swift. The main goal is to practice Clean Architecture Development.
Swift
60
star
19

KataTODOApiClientJava

TODO API Client Kata for Java Developers. The main goal is to practice integration testing using MockWebServer.
Java
53
star
20

play-framework-kotlin

This repository is to show how to create an Play Framework project using Kotlin
Kotlin
48
star
21

KataContactsKotlin

KataContacts written in Kotlin. The main goal is to practice Clean Architecture Development
Kotlin
48
star
22

Hagu

Gradle plugin to enable Kotlin build configuration secrets for Kotlin, Kotlin-Native / Multiplatform.
Kotlin
47
star
23

KataSuperHeroesJetpack

SuperHeroes kata for Jetpack Developers in Kotlin
Kotlin
47
star
24

WeakDelegate

WeakReference property delegate proposal
Kotlin
47
star
25

KotlinMultiplatformApp

Application example using Kotlin multiplatform
Objective-C
43
star
26

MaxibonKataKotlin

Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.
Kotlin
43
star
27

SuperHeroesKotlin

An example of architecture in kotlin
Kotlin
41
star
28

Reddo

Show messages in a 16x32 led matrix using a Raspberry Pi.
Java
39
star
29

SpringBootKotlin

Spring boot Kotlin example
Kotlin
38
star
30

BothamNetworking

Networking Framework written in Swift.
Swift
26
star
31

MaxibonKataIOS

Maxibon kata for iOS Developers. The main goal is to practice property based testing.
Swift
26
star
32

ControlFlowUI

A library that add control flow functionality to SwitUI, using the power of @functionBuilder and ViewBuilder.
Swift
25
star
33

AndroidAnimations

This is the project where we will analyze study and put into practice how to work with animations in Android
Kotlin
22
star
34

KataTODOApiClientIOS

TODO API Client Kata for iOS Developers. The main goal is to practice integration testing using Nocilla and Nimble.
Swift
21
star
35

FoursquareTop

Code for the app made as part of the NSSpain 2016 talk
Swift
21
star
36

KataSuperHeroesCompose

Super Heroes Kata implemented using Jetpack Compose and Screenshot Testing.
Kotlin
19
star
37

iOSBasicTraining

Code associated to the first level of our iOS Training.
Swift
17
star
38

KataCoroutines

Transform this game-of-life sequential implementation to a parallel version with Kotlin coroutines
Kotlin
16
star
39

ReactNativePlayground

Playground used to learn and experiment with React Native πŸš€
TypeScript
16
star
40

KataScreenshotIOS

Screenshot Kata for iOS Developers. The main goal is to practice UI Testing
Swift
16
star
41

KataLogInLogOutKotlin

Solution for the testing kata imparted during the mobile testing training written in Kotlin
Kotlin
15
star
42

MarvelApiClient

Marvel Api Client written in Swift.
Swift
14
star
43

ktlint-sbt

Ktlint Sbt Plugin
Scala
13
star
44

kodein-sample-testing

This repository aims to be a small example of how to use Kodein to provide different implementations for production code and testing code
Kotlin
11
star
45

KataSuperHeroesSpringBoot

This is a spring boot kata for the courses.
Kotlin
10
star
46

EndZone

Swift
10
star
47

hangout-slack-import

Script that help you import your google hangout chat history to Slack
Python
9
star
48

MagicCounterKataAndroid

Magic counter kata for Android developers. The main goal is to unit and integration testing.
Kotlin
9
star
49

KataSuperHeroesReactNative

Super Heroes Kata for React Native Developers in Typescript.
TypeScript
8
star
50

HotSwitchLocalizationTest

Android app implemented to show how to change the language and country used by our apps in runtime without restarting thea app. This only works for API >= 17. For API < 17 the localization change is not applied.
Kotlin
6
star
51

boilerplate-wars

Swift
6
star
52

TestingPlayFramework

Example repository used to show how to test a Play Framework application
Scala
4
star
53

KataContactsCSharp

C#
4
star
54

KataLogInLogOutSwift

Solution for the testing kata imparted during the mobile testing training written in Swift
Swift
4
star
55

LitElementPlayground

Playground used to learn and experiment with Lit Element and Web development
JavaScript
3
star
56

BasicTextFieldKeyboardBug

Repository used to show how to reproduce a bug we've found in BasicTextField component.
Kotlin
3
star
57

ExtensionProtocolforStruct

Swift
3
star
58

infrared-wifi-bridge-led

Open Hardware, Infrared Wifi bridge with an integrated LED Strip.
C
2
star
59

MaxibonKataCSharp

C#
2
star
60

SwiftMonad

Swift
2
star
61

KarumiJekyllTheme

Karumi Official Jekyll Template
CSS
2
star
62

KataSuperHeroesSwiftUI

Swift
2
star
63

MagicCounterKataIOS

Magic counter kata for iOS developers. The main goal is to unit and integration testing.
Swift
1
star