Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rubensousa/Decorator
Decorator is an Android library that helps creating composable margins and dividers in RecyclerViews
https://github.com/rubensousa/Decorator
Last synced: 8 days ago
JSON representation
Decorator is an Android library that helps creating composable margins and dividers in RecyclerViews
- Host: GitHub
- URL: https://github.com/rubensousa/Decorator
- Owner: rubensousa
- License: apache-2.0
- Created: 2020-04-14T09:18:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-11-15T22:50:42.000Z (about 2 years ago)
- Last Synced: 2024-11-06T06:12:00.273Z (10 days ago)
- Language: Kotlin
- Homepage:
- Size: 739 KB
- Stars: 536
- Watchers: 17
- Forks: 38
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-list - rubensousa/Decorator - Decorator is an Android library that helps creating composable margins and dividers in RecyclerViews (Kotlin)
README
# 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
```groovy
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:
```kotlin
recyclerView.addItemDecoration(decoration)
```## Margin decorations
These decorations draw a margin between the items.
#### LinearMarginDecoration
```kotlin
recyclerView.addItemDecoration(LinearMarginDecoration(
leftMargin = resources.dpToPx(16),
topMargin = resources.dpToPx(16),
rightMargin = resources.dpToPx(16),
bottomMargin = resources.dpToPx(16),
orientation = RecyclerView.VERTICAL
))
``````kotlin
recyclerView.addItemDecoration(LinearMarginDecoration.createVertical(
verticalMargin = resources.dpToPx(16)
))
```#### GridMarginDecoration
```kotlin
recyclerView.addItemDecoration(GridMarginDecoration(
margin = resources.dpToPx(16),
columnProvider = object : ColumnProvider {
override fun getNumberOfColumns(): Int = 3
},
orientation = RecyclerView.VERTICAL
))
```#### GridSpanDecoration
```kotlin
recyclerView.addItemDecoration(GridSpanMarginDecoration(
margin = resources.dpToPx(8),
gridLayoutManager = gridLayoutManager
))
```## Divider decorations
These decorations draw a divider between the items
#### LinearDividerDecoration
```kotlin
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
```kotlin
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
```kotlin
recyclerView.addItemDecoration(LinearBoundsMarginDecoration.create(
margin = resources.dpToPx(8),
orientation = RecyclerView.VERTICAL
))
```#### GridBoundsMarginDecoration
```kotlin
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:
```kotlin
// 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:
```kotlin
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.