title: Get the column number where a view is on a GridLayoutManager
body: <p>I have different types of items being rendered in a <code>RecyclerView</code> using a <code>GridLayoutManager</code> with dynamic column number. The problem is, I have an <code>RecyclerView.ItemDecoration</code> which is to be applied for let's say <code>Type A</code> items only. this <code>RecyclerView.ItemDecoration</code> adds margin to the left/start to those items on the left hand side column, and margin to the right/end to those items on the right hand side column. It's basically to make the items look a bit more centered and so so stretched (this is used on a tablet/landscape mode). The <code>RecyclerView</code> grid looks something like this:</p>
  
  <pre><code>| A | | A |
  | A | | A |
     | B |
  | A | | A |
  | A | | A |
     | B |
  | A | | A |
  </code></pre>
  
  <p>The <code>ItemDecoration</code> looks like this:</p>
  
  <pre><code>class TabletGridSpaceItemDecoration(private val space: Int) : RecyclerView.ItemDecoration() {
  
      override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) = with(outRect) {
          val isTypeAItemLayout = view.findViewById&lt;ConstraintLayout&gt;(R.id.type_a_item_container) != null
  
          if (isTypeAItemLayout) {
              val adapterPosition = parent.getChildAdapterPosition(view)
  
              if (adapterPosition % 2 == 0) {
                  left = space
                  right = 0
              } else {
                  left = 0
                  right = space
              }
          }
      }
  }
  </code></pre>
  
  <p>The problem with this decorator is that after the first <code>type B</code> item on the list, the indexes get screwed for the next <code>type A</code> items. So first item after <code>B</code> according to the example provided will have <code>adapterPosition == 5</code>, so according to <code>TabletGridSpaceItemDecoration</code> the marging should be added to the right, which is incorrect.</p>
  
  <ul>
  <li><p>I tried having a <code>HashMap</code> to keep the <code>adapterPosition</code> and the real position of the item, i.e. the position it would have on the adapter ignoring non <code>type A</code> items. This has some other problems I won't get too much into details, but it's doesn't feel the correct way to do it.</p></li>
  <li><p>Another thing I tried is to check the location on the screen of the view (more to the left or to the right) the item decoration is going to be applied. The problem with this is that the view has not been rendered yet when this decorator is ran. Adding a <code>ViewTreeObserver.OnGlobalLayoutListener</code> on the view is worthless since by the time the view is rendered the item decoration is already applied, which makes no effect on the view.</p></li>
  </ul>
  
  <p><strong>What I would like to do is to check whether or not an item is in "column 0" or "column 1" and add margin accordingly.</strong></p>
  
  <p>I don't know how is this possible and didn't find the way to do it looking at what the <code>GridLayoutManager</code> provides, that can be accessed by <code>parent.layoutManager as GridLayoutManager</code>.</p>
  
  <p>Any ideas? Thanks</p>