Skip to content

Commit

Permalink
Improving MarginItemDecoration
Browse files Browse the repository at this point in the history
  • Loading branch information
cmorigaki committed May 25, 2020
1 parent ef84cf4 commit 8bf7277
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import androidx.lifecycle.observe
import br.com.recipebook.coreandroid.image.ImageResolver
import br.com.recipebook.designsystem.ListMarginItemDecoration
import br.com.recipebook.navigation.MainNavigator
import br.com.recipebook.navigation.R
import br.com.recipebook.navigation.intent.RecipeDetailIntent
import br.com.recipebook.recipecollection.databinding.RecipeCollectionActivityBinding
import br.com.recipebook.recipecollection.presentation.RecipeCollectionViewAction
import br.com.recipebook.recipecollection.presentation.RecipeCollectionViewModel
import br.com.recipebook.utilityandroid.MarginItemDecoration
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import br.com.recipebook.utilityandroid.MarginItemDecoration

class ListMarginItemDecoration(resources: Resources, spanCount: Int = 1) :
MarginItemDecoration(
space = resources.getDimensionPixelSize(R.dimen.margin_normal_100),
spaceSize = resources.getDimensionPixelSize(R.dimen.margin_normal_100),
spanCount = spanCount
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@ package br.com.recipebook.utilityandroid

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView

open class MarginItemDecoration(private val space: Int, private val spanCount: Int = 1) :
RecyclerView.ItemDecoration() {
open class MarginItemDecoration(
private val spaceSize: Int,
private val spanCount: Int = 1,
private val orientation: Int = GridLayoutManager.VERTICAL
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect, view: View,
parent: RecyclerView, state: RecyclerView.State
) {
with(outRect) {
if (parent.getChildAdapterPosition(view) < spanCount) {
top = space
if (orientation == GridLayoutManager.VERTICAL) {
if (parent.getChildAdapterPosition(view) < spanCount) {
top = spaceSize
}
if (parent.getChildAdapterPosition(view) % spanCount == 0) {
left = spaceSize
}
} else {
if (parent.getChildAdapterPosition(view) < spanCount) {
left = spaceSize
}
if (parent.getChildAdapterPosition(view) % spanCount == 0) {
top = spaceSize
}
}
if (parent.getChildAdapterPosition(view) % spanCount == 0) {
left = space
}
right = space
bottom = space

right = spaceSize
bottom = spaceSize
}
}
}

0 comments on commit 8bf7277

Please sign in to comment.