title: How can I properly center the first and last items in a horizontal RecyclerView
body: <p>StackOverflow contains a lot of questions like this one, but so far absolutely no solution works 100%.</p>
  <p>I tried the solutions for these:</p>
  <p><a href="https://stackoverflow.com/q/56751398/524695">RecyclerView ItemDecoration - How to draw a different width divider for every viewHolder?</a></p>
  <p><a href="https://stackoverflow.com/q/50425002/524695">first item center aligns in SnapHelper in RecyclerView</a></p>
  <p><a href="https://stackoverflow.com/q/44662420/524695">Horizontally center first item of RecyclerView</a></p>
  <p><a href="https://stackoverflow.com/q/44766186/524695">LinearSnapHelper doesn&#39;t snap on edge items of RecyclerView</a></p>
  <p><a href="https://stackoverflow.com/q/29117916/524695">Android Centering Item in RecyclerView</a></p>
  <p><a href="https://stackoverflow.com/q/30259741/524695">How to make recycler view start adding items from center?</a></p>
  <p><a href="https://stackoverflow.com/q/53483268/524695">How to have RecyclerView snapped to center and yet be able to scroll to all items, while the center is &quot;selected&quot;?</a></p>
  <p><a href="https://stackoverflow.com/q/51495651/524695">How to snap to particular position of LinearSnapHelper in horizontal RecyclerView?</a></p>
  <hr />
  <p>And every time the first item fails to be centered correctly.</p>
  <p>Sample code of what I am using</p>
  <pre><code>    recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
          @Override
          public void getItemOffsets(
              @NonNull Rect outRect,
              @NonNull View view,
              @NonNull RecyclerView parent,
              @NonNull RecyclerView.State state
          ) {
  
              super.getItemOffsets(outRect, view, parent, state);
  
              final int count = state.getItemCount();
              final int position = parent.getChildAdapterPosition(view);
  
              if (position == 0 || position == count - 1) {
  
                  int offset = (int) (parent.getWidth() * 0.5f - view.getWidth() * 0.5f);
  
                  if (position == 0) {
                      setupOutRect(outRect, offset, true);
                  } else if (position == count - 1) {
                      setupOutRect(outRect, offset, false);
                  }
  
              }
  
          }
  
          private void setupOutRect(Rect rect, int offset, boolean start) {
              if (start) {
                  rect.left = offset;
              } else {
                  rect.right = offset;
              }
          }
  
      });
  </code></pre>
  <p>After investigating I discovered that is because at the time of the <code>getItemOffsets</code> the <code>view.getWidth</code> is 0, it hasn't been measured yet.</p>
  <p>I tried to force it to be measured, but every single time it gives an incorrect size, nothing like the actual size it occupies, it is smaller.</p>
  <p>I also tried to use the <code>addOnGlobalLayoutListener</code> trick, but by the time it is called and has the correct width, the <code>outRect</code> was already consumed, so it is lost.</p>
  <p>I do not want to set any fixed sizes because the items in the <code>RecyclerView</code> can have different sizes, so setting its padding in advance is not an option.</p>
  <p>I also do not want to add &quot;ghost&quot; items to fill the space and those also don't work well for the scrolling experience.</p>
  <p>How can I get this working properly?</p>
  <p>Ideally the <code>ItemDecorator</code> method looks to be the best, but it falls flat for the first item right away.</p>