Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 15th, 2018 84 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. class ItemOffsetDecoration(@DimenRes val offsetId: Int) : RecyclerView.ItemDecoration() {
  2.  
  3.     override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
  4.         super.getItemOffsets(outRect, view, parent, state)
  5.  
  6.         val offset = view.context.resources.getDimensionPixelSize(offsetId)
  7.         val position = parent.getChildAdapterPosition(view)
  8.         val items = parent.adapter?.itemCount ?: 0
  9.  
  10.         if (parent.layoutManager is GridLayoutManager) {
  11.             val columns = (parent.layoutManager as GridLayoutManager).spanCount
  12.             val rows = (items / columns)
  13.  
  14.             val column = getColumn(position, columns)
  15.             val row = getRow(position, columns)
  16.  
  17.             val topOffset = if (row == 1) offset else offset / 2
  18.             val leftOffset = if (column == 1) offset else offset / 2
  19.  
  20.             val bottomOffset = if (row == rows) offset else offset / 2
  21.             val rightOffset = if (column == columns) offset else offset / 2
  22.  
  23.             outRect.set(leftOffset, topOffset, rightOffset, bottomOffset)
  24.  
  25.         } else if (parent.layoutManager is LinearLayoutManager) {
  26.             val top = if (position > 0) 0 else offset
  27.  
  28.             outRect.set(offset, top, offset, offset)
  29.         }
  30.     }
  31.  
  32.     private fun getRow(position: Int, columns: Int) =
  33.         Math.ceil((position.toDouble() + 1.0) / columns.toDouble()).toInt()
  34.  
  35.     private fun getColumn(position: Int, columns: Int) = (position % columns) + 1
  36.  
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top