Created
April 16, 2021 04:20
-
-
Save widiarifki/1dd77d16b780e52bdbacffc546ff3246 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.pengadaan.helper | |
import android.graphics.Rect | |
import android.view.View | |
import androidx.recyclerview.widget.GridLayoutManager | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import androidx.recyclerview.widget.RecyclerView | |
import androidx.recyclerview.widget.StaggeredGridLayoutManager | |
import kotlin.math.roundToInt | |
class SpacedItemDecoration : RecyclerView.ItemDecoration { | |
private var space = 0 | |
private var mView : View? = null | |
constructor() | |
constructor(space: Int) : super() { | |
this.space = space | |
} | |
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { | |
super.getItemOffsets(outRect, view, parent, state) | |
mView = view | |
val rvLayoutManager = parent.layoutManager | |
val rvItemCount = state.itemCount | |
val adapterItemCount = parent.adapter?.itemCount ?: 0 | |
var itemPosition = parent.getChildAdapterPosition(view) | |
if(rvItemCount > adapterItemCount){ | |
if(itemPosition == 0){ | |
return | |
}else{ | |
itemPosition -= 1 | |
} | |
} | |
if (rvLayoutManager is GridLayoutManager) { | |
setItemGridOffset(outRect, rvLayoutManager.orientation, rvLayoutManager.spanCount, adapterItemCount, itemPosition) | |
} else if (rvLayoutManager is StaggeredGridLayoutManager) { | |
setItemStaggeredGridOffset(outRect, rvLayoutManager.orientation, rvLayoutManager.spanCount, adapterItemCount, itemPosition) | |
} else if (rvLayoutManager is LinearLayoutManager) { | |
setItemLinearOffset(outRect, rvLayoutManager.orientation, itemPosition) | |
} | |
} | |
private fun setItemGridOffset(outRect: Rect, layoutOrientation: Int, spanCount: Int, adapterItemCount: Int, itemPosition: Int){ | |
val position = itemPosition + 1 | |
val divideByTwo = space/2 | |
if(layoutOrientation == GridLayoutManager.VERTICAL){ | |
if(position.rem(spanCount) != 0){ | |
outRect.right = divideByTwo | |
}else{ | |
outRect.left = divideByTwo | |
} | |
if((adapterItemCount.rem(spanCount) != 0 && position < adapterItemCount) | |
|| (adapterItemCount.rem(spanCount) == 0 && position <= (adapterItemCount - spanCount))){ // Not in the last row | |
outRect.bottom = space | |
} | |
}else{ | |
if(position.rem(spanCount) != 0){ | |
outRect.bottom = divideByTwo | |
}else{ | |
outRect.top = divideByTwo | |
} | |
if((adapterItemCount.rem(spanCount) != 0 && position < adapterItemCount) | |
|| (adapterItemCount.rem(spanCount) == 0 && position <= (adapterItemCount - spanCount))){ // Not in the last column | |
outRect.right = space | |
} | |
} | |
} | |
private fun setItemLinearOffset(outRect: Rect, layoutOrientation: Int, itemPosition: Int){ | |
if(layoutOrientation == GridLayoutManager.VERTICAL){ | |
if(itemPosition == 0) { | |
val littleSpare = (space * 0.5).roundToInt() | |
outRect.top = space + littleSpare | |
} else { | |
outRect.top = space | |
} | |
}else{ | |
if(itemPosition == 0) { | |
return | |
} | |
outRect.left = space | |
} | |
} | |
private fun setItemStaggeredGridOffset(outRect: Rect, layoutOrientation: Int, spanCount: Int, itemCount: Int, childLayoutPosition: Int){ | |
val position = childLayoutPosition + 1 | |
val layoutParams = mView?.layoutParams as StaggeredGridLayoutManager.LayoutParams | |
val spanIndex = layoutParams.spanIndex | |
val divideByTwo = space/2 | |
if(layoutOrientation == GridLayoutManager.VERTICAL){ | |
if(spanIndex == 1){ | |
outRect.left = divideByTwo; | |
} else{ | |
outRect.right = divideByTwo; | |
} | |
if((itemCount.rem(spanCount) != 0 && position < itemCount) | |
|| (itemCount.rem(spanCount) == 0 && position <= (itemCount - spanCount))){ // Not in the last row | |
outRect.bottom = space | |
} | |
}else{ | |
if(spanIndex == 1){ | |
outRect.bottom = divideByTwo; | |
} else{ | |
outRect.top = divideByTwo; | |
} | |
if((itemCount.rem(spanCount) != 0 && position < itemCount) | |
|| (itemCount.rem(spanCount) == 0 && position <= (itemCount - spanCount))){ // Not in the last column | |
outRect.right = space | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment