{"id":13611356,"url":"https://github.com/rubensousa/Decorator","last_synced_at":"2025-04-13T04:34:06.108Z","repository":{"id":38327690,"uuid":"255563870","full_name":"rubensousa/Decorator","owner":"rubensousa","description":"Decorator is an Android library that helps creating composable margins and dividers in RecyclerViews","archived":false,"fork":false,"pushed_at":"2022-11-15T22:50:42.000Z","size":757,"stargazers_count":536,"open_issues_count":2,"forks_count":38,"subscribers_count":17,"default_branch":"master","last_synced_at":"2024-11-06T06:12:00.273Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rubensousa.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-14T09:18:45.000Z","updated_at":"2024-11-05T00:31:58.000Z","dependencies_parsed_at":"2023-01-21T22:45:53.253Z","dependency_job_id":null,"html_url":"https://github.com/rubensousa/Decorator","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubensousa%2FDecorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubensousa%2FDecorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubensousa%2FDecorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubensousa%2FDecorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rubensousa","download_url":"https://codeload.github.com/rubensousa/Decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223568184,"owners_count":17166613,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T19:01:54.496Z","updated_at":"2024-11-07T18:30:21.176Z","avatar_url":"https://github.com/rubensousa.png","language":"Kotlin","funding_links":[],"categories":["Kotlin"],"sub_categories":[],"readme":"# Decorator\n\nIn 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.\n\nWhile this works for many applications, sometimes the margins inside the layout files shouldn't be applied in every screen.\n\nDecorator is a library that helps creating composable margins and dividers in RecyclerViews.\n\n## Install\n\n```groovy\nimplementation 'com.rubensousa:decorator:2.1.0'\n```\n\n## How to use\n\nJust create one of the decorations provided in the next examples and add them to the RecyclerView using:\n\n```kotlin\nrecyclerView.addItemDecoration(decoration)\n```\n\n## Margin decorations\n\nThese decorations draw a margin between the items.\n\n#### LinearMarginDecoration\n\n\u003cimg src=\"examples/linearmargin.png\" width=300\u003e\u003c/img\u003e\n\n```kotlin\nrecyclerView.addItemDecoration(LinearMarginDecoration(\n    leftMargin = resources.dpToPx(16),\n    topMargin = resources.dpToPx(16),\n    rightMargin = resources.dpToPx(16),\n    bottomMargin = resources.dpToPx(16),\n    orientation = RecyclerView.VERTICAL\n))\n```\n\n```kotlin\nrecyclerView.addItemDecoration(LinearMarginDecoration.createVertical(\n    verticalMargin = resources.dpToPx(16)\n))\n```\n\n#### GridMarginDecoration\n\n\u003cimg src=\"examples/gridmargin.png\" width=300\u003e\u003c/img\u003e\n\n```kotlin\nrecyclerView.addItemDecoration(GridMarginDecoration(\n    margin = resources.dpToPx(16),\n    columnProvider = object : ColumnProvider {\n        override fun getNumberOfColumns(): Int = 3\n    },\n    orientation = RecyclerView.VERTICAL\n))\n```\n\n\n#### GridSpanDecoration\n\n\u003cimg src=\"examples/gridspanmargin.png\" width=300\u003e\u003c/img\u003e\n\n```kotlin\nrecyclerView.addItemDecoration(GridSpanMarginDecoration(\n    margin = resources.dpToPx(8),\n    gridLayoutManager = gridLayoutManager\n))\n```\n\n## Divider decorations\n\nThese decorations draw a divider between the items\n\n#### LinearDividerDecoration\n\n\u003cimg src=\"examples/lineardivider.png\" width=300\u003e\u003c/img\u003e\n\n\n```kotlin\nrecyclerView.addItemDecoration(LinearDividerDecoration.create(\n    color = ContextCompat.getColor(context, R.color.colorDivider),\n    size = resources.dpToPx(4),\n    leftMargin = resources.dpToPx(64),\n    topMargin = resources.dpToPx(16),\n    rightMargin = resources.dpToPx(64),\n    bottomMargin = resources.dpToPx(16),\n    orientation = RecyclerView.VERTICAL\n))\n```\n\n#### GridDividerDecoration\n\n\u003cimg src=\"examples/griddivider.png\" width=300\u003e\u003c/img\u003e\n\n```kotlin\nrecyclerView.addItemDecoration(GridDividerDecoration.create(\n    color = ContextCompat.getColor(context, R.color.colorDivider),\n    size = resources.dpToPx(2),\n    columnProvider = object : ColumnProvider {\n        override fun getNumberOfColumns(): Int = 3\n    },\n    widthMargin = resources.dpToPx(16),\n    heightMargin = resources.dpToPx(16),\n    orientation = RecyclerView.VERTICAL\n))\n```\n\n## Bounds margin decorations\n\nThese decorations are similar to the margin decorations,  \nbut they only draw a margin in the bounds of the list.\n\n#### LinearBoundsMarginDecoration\n\n\u003cimg src=\"examples/linearboundsmargin.png\" width=300\u003e\u003c/img\u003e\n\n```kotlin\nrecyclerView.addItemDecoration(LinearBoundsMarginDecoration.create(\n    margin = resources.dpToPx(8),\n    orientation = RecyclerView.VERTICAL\n))\n```\n\n#### GridBoundsMarginDecoration\n\n\u003cimg src=\"examples/gridboundsmargin.png\" width=300\u003e\u003c/img\u003e\n\n```kotlin\nrecyclerView.addItemDecoration(GridBoundsMarginDecoration.create(\n    leftMargin = resources.dpToPx(64),\n    topMargin = resources.dpToPx(16),\n    rightMargin = resources.dpToPx(64),\n    bottomMargin = resources.dpToPx(16),\n    columnProvider = object : ColumnProvider {\n        override fun getNumberOfColumns(): Int = 3\n    }\n))\n```\n\n## Composing decorations\n\nIf you want to apply multiple decorations, you just need to add the decorations in the correct order.\n\nExample for a vertical RecyclerView:\n\n```kotlin\n// Add a vertical margin to the first and last items. \n// The first item will have a top margin and the last item a bottom margin\nrecyclerView.addItemDecoration(\n    LinearBoundsMarginDecoration.createVertical(\n        verticalMargin = edgeDecorationSize\n    )\n)\n\n// Add a vertical margin between all items\nrecyclerView.addItemDecoration(\n    LinearMarginDecoration.createVertical(\n        verticalMargin = marginDecorationSize\n    )\n)\n\n// Finally, add a divider between the items while respecting the previous margins:\nrecyclerView.addItemDecoration(LinearDividerDecoration.create(\n    color = ContextCompat.getColor(context, R.color.colorDivider),\n    size = fragment.dpToPx(2)\n))\n\n```\n\nYou can also decide if an item at a given position should have a decoration applied:\n\n```kotlin\n\ndecoration.setDecorationLookup(object : DecorationLookup {\n    // Don't apply the decoration on position 4\n    override fun shouldApplyDecoration(viewHolder: RecyclerView.ViewHolder, itemCount: Int): Boolean {\n        return viewHolder.layoutPosition != 4\n    }\n})\n```\n\n## License\n\n    Copyright 2021 Rúben Sousa\n    \n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n    \n        http://www.apache.org/licenses/LICENSE-2.0\n    \n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubensousa%2FDecorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubensousa%2FDecorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubensousa%2FDecorator/lists"}