title: Implementing RecyclerView Zoom with Dividers
body: <p>I have made a recyclerview that zooms, but the divider does not zoom with the view, so the divider lines stay the same height even though the content of the recyclerview is zooming. How do I make the divider zoom with the recyclerview? Thanks in advance!</p>
  
  <p>Main Activity:</p>
  
  <pre><code>recyclerView = (ZoomRecyclerView) findViewById(R.id.recyclerView);
  
  LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
  linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  recyclerView.setLayoutManager(linearLayoutManager);
  recyclerView.setHasFixedSize(true);
  
  recyclerViewAdapter = new RecyclerViewAdapter(listOfData);
  if (recyclerView!= null) {
      recyclerView.setAdapter(recyclerViewAdapter);
  }
  
  Drawable divider = ContextCompat.getDrawable(this, R.drawable.divider);
  RecyclerViewDecorator recyclerViewDecorator = new RecyclerViewDecorator(divider);
  recyclerView.addItemDecoration(recyclerViewDecorator);
  </code></pre>
  
  <p>divider.xml Drawable File:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle"&gt;
      &lt;size android:height="2dp"&gt;&lt;/size&gt;
      &lt;solid android:color="@android:color/black"&gt;&lt;/solid&gt;
  &lt;/shape&gt;
  </code></pre>
  
  <p>Recycler View Decorator:</p>
  
  <pre><code>public class RecyclerViewDecorator extends RecyclerView.ItemDecoration {
  
  private Drawable divider;
  
  public RecyclerViewDecorator(Drawable divider) {
      this.divider = divider;
  }
  
  @Override
  public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
      int dividerLeft = parent.getPaddingLeft();
      int dividerRight = parent.getWidth() - parent.getPaddingRight();
      int dividerTop;
      int dividerBottom;
  
      int childCount = parent.getChildCount();
      for (int i = 0; i &lt; childCount - 1; i++) {
          View child = parent.getChildAt(i);
  
          RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
  
          dividerTop = child.getBottom() + params.bottomMargin;
          dividerBottom = dividerTop + divider.getIntrinsicHeight();
  
          divider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
          divider.draw(canvas);
      }
  }
  
  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
      super.getItemOffsets(outRect, view, parent, state);
  
      if (parent.getChildAdapterPosition(view) == 0) {
          return;
      }
  
      outRect.top = divider.getIntrinsicHeight();
  }
  }
  </code></pre>
  
  <p>Zooming Recycler View:</p>
  
  <pre><code>public class ZoomRecyclerView extends RecyclerView {
  private ScaleGestureDetector scaleGestureDetector;
  private static final String TAG = ZoomRecyclerView.class.getSimpleName();
  
  private float scaleFactor = 1.f;
  private static final float minScale = 1.0f;
  private static final float maxScale = 3.0f;
  
  public ZoomRecyclerView(Context context, AttributeSet attrs) {
      super(context, attrs);
      scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.OnScaleGestureListener() {
          @Override
          public boolean onScale(ScaleGestureDetector detector) {
              scaleFactor *= detector.getScaleFactor();
  
              //makes sure user does not zoom in or out past a certain amount
              scaleFactor = Math.max(minScale, Math.min(scaleFactor, maxScale));
  
              //refresh the view and compute the size of the view in the screen
              requestLayout();
  
              return true;
          }
  
          @Override
          public boolean onScaleBegin(ScaleGestureDetector detector) {
              return true;
          }
  
          @Override
          public void onScaleEnd(ScaleGestureDetector detector) {
  
          }
      });
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      super.onTouchEvent(event);
  
      //notify the scaleGestureDetector that an event has happened
      scaleGestureDetector.onTouchEvent(event);
  
      return true;
  }
  
  @Override
  protected void dispatchDraw(@NonNull Canvas canvas) {
      canvas.save();
  
      //scales the display, centered on where the user is touching the display
      canvas.scale(scaleFactor, scaleFactor, scaleGestureDetector.getFocusX(), scaleGestureDetector.getFocusY());
  
      super.dispatchDraw(canvas);
      canvas.restore();
  
  }
  
  }
  </code></pre>