• Stars
    star
    533
  • Rank 83,238 (Top 2 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated about 2 years ago

Reviews

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

Repository Details

Decorator is an Android library that helps creating composable margins and dividers in RecyclerViews

Decorator

In Android, when you work with RecyclerViews, the standard way of working with paddings and margins is to work with fixed dimensions inside of the layout files.

While this works for many applications, sometimes the margins inside the layout files shouldn't be applied in every screen.

Decorator is a library that helps creating composable margins and dividers in RecyclerViews.

Install

implementation 'com.rubensousa:decorator:2.1.0'

How to use

Just create one of the decorations provided in the next examples and add them to the RecyclerView using:

recyclerView.addItemDecoration(decoration)

Margin decorations

These decorations draw a margin between the items.

LinearMarginDecoration

recyclerView.addItemDecoration(LinearMarginDecoration(
    leftMargin = resources.dpToPx(16),
    topMargin = resources.dpToPx(16),
    rightMargin = resources.dpToPx(16),
    bottomMargin = resources.dpToPx(16),
    orientation = RecyclerView.VERTICAL
))
recyclerView.addItemDecoration(LinearMarginDecoration.createVertical(
    verticalMargin = resources.dpToPx(16)
))

GridMarginDecoration

recyclerView.addItemDecoration(GridMarginDecoration(
    margin = resources.dpToPx(16),
    columnProvider = object : ColumnProvider {
        override fun getNumberOfColumns(): Int = 3
    },
    orientation = RecyclerView.VERTICAL
))

GridSpanDecoration

recyclerView.addItemDecoration(GridSpanMarginDecoration(
    margin = resources.dpToPx(8),
    gridLayoutManager = gridLayoutManager
))

Divider decorations

These decorations draw a divider between the items

LinearDividerDecoration

recyclerView.addItemDecoration(LinearDividerDecoration.create(
    color = ContextCompat.getColor(context, R.color.colorDivider),
    size = resources.dpToPx(4),
    leftMargin = resources.dpToPx(64),
    topMargin = resources.dpToPx(16),
    rightMargin = resources.dpToPx(64),
    bottomMargin = resources.dpToPx(16),
    orientation = RecyclerView.VERTICAL
))

GridDividerDecoration

recyclerView.addItemDecoration(GridDividerDecoration.create(
    color = ContextCompat.getColor(context, R.color.colorDivider),
    size = resources.dpToPx(2),
    columnProvider = object : ColumnProvider {
        override fun getNumberOfColumns(): Int = 3
    },
    widthMargin = resources.dpToPx(16),
    heightMargin = resources.dpToPx(16),
    orientation = RecyclerView.VERTICAL
))

Bounds margin decorations

These decorations are similar to the margin decorations,
but they only draw a margin in the bounds of the list.

LinearBoundsMarginDecoration

recyclerView.addItemDecoration(LinearBoundsMarginDecoration.create(
    margin = resources.dpToPx(8),
    orientation = RecyclerView.VERTICAL
))

GridBoundsMarginDecoration

recyclerView.addItemDecoration(GridBoundsMarginDecoration.create(
    leftMargin = resources.dpToPx(64),
    topMargin = resources.dpToPx(16),
    rightMargin = resources.dpToPx(64),
    bottomMargin = resources.dpToPx(16),
    columnProvider = object : ColumnProvider {
        override fun getNumberOfColumns(): Int = 3
    }
))

Composing decorations

If you want to apply multiple decorations, you just need to add the decorations in the correct order.

Example for a vertical RecyclerView:

// Add a vertical margin to the first and last items. 
// The first item will have a top margin and the last item a bottom margin
recyclerView.addItemDecoration(
    LinearBoundsMarginDecoration.createVertical(
        verticalMargin = edgeDecorationSize
    )
)

// Add a vertical margin between all items
recyclerView.addItemDecoration(
    LinearMarginDecoration.createVertical(
        verticalMargin = marginDecorationSize
    )
)

// Finally, add a divider between the items while respecting the previous margins:
recyclerView.addItemDecoration(LinearDividerDecoration.create(
    color = ContextCompat.getColor(context, R.color.colorDivider),
    size = fragment.dpToPx(2)
))

You can also decide if an item at a given position should have a decoration applied:

decoration.setDecorationLookup(object : DecorationLookup {
    // Don't apply the decoration on position 4
    override fun shouldApplyDecoration(viewHolder: RecyclerView.ViewHolder, itemCount: Int): Boolean {
        return viewHolder.layoutPosition != 4
    }
})

License

Copyright 2021 Rúben Sousa

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

GravitySnapHelper

A SnapHelper that snaps a RecyclerView to an edge.
Java
4,987
star
2

ViewPagerCards

ViewPager cards inspired by Duolingo
Java
4,065
star
3

PreviewSeekBar

A SeekBar suited for showing a preview of something. As seen in Google Play Movies.
Java
3,437
star
4

FloatingToolbar

A toolbar that morphs from a FloatingActionButton
Java
1,551
star
5

BottomSheetBuilder

A simple library that creates BottomSheets according to the Material Design specs
Java
558
star
6

RaiflatButton

A raised button that lowers down to 0dp of elevation
Java
330
star
7

RecyclerViewNestedExample

From: https://rubensousa.com/2019/08/16/nested_recyclerview_part1/ and https://rubensousa.com/2019/08/27/saving_scroll_state_of_nested_recyclerviews/
Kotlin
165
star
8

BottomSheetExample

A sample project with the new BottomSheet classes from the android support library
Java
129
star
9

Transitions

A sample that showcases some transitions
Java
118
star
10

DpadRecyclerView

A RecyclerView built for Android TV with Compose in mind and as a replacement for Leanback's BaseGridView.
Kotlin
103
star
11

SlidingTabs

Tabs created with the new android.support.design.widget.TabLayout
Java
25
star
12

StackView

A view that arranges its children in the form of a stack
Java
16
star
13

lista

Lista helps you building composable sections and nested lists in RecyclerViews.
Kotlin
14
star
14

LoadMoreAdapter

A RecyclerView adapter that offers support to loading more items
Java
9
star
15

AndroidGithubPackage

Sample Android library that's published to GitHub Packages using a GitHub Action
Java
5
star
16

FragmentContainerViewIssue

https://issuetracker.google.com/issues/146359827
Kotlin
3
star
17

TabLayoutAdapter

A helper library to easily create tabs with TabLayout
Java
3
star
18

NavigationViewExample

An example app with NavigationView of the android.support.design library
Java
3
star
19

CropView

A simple view that selects an area for cropping
Java
3
star
20

WorkManagerKitkatBug

https://issuetracker.google.com/issues/122578012
Kotlin
2
star
21

rubensousa.github.io

SCSS
2
star
22

Android_8.0_Accessibility_Crash

Kotlin
2
star
23

WorkManagerConcurrent

https://issuetracker.google.com/issues/121345393
Kotlin
1
star
24

Mieti

Aplicação para o cálculo da média do Mestrado Integrado em Engenharia de Telecomunicações e Informática da Universidade do Minho.
Java
1
star
25

adventofcode2017

Solutions for Advent of Code
Kotlin
1
star
26

StateSaverIssue

Java
1
star