Use LayoutManager to get RecyclerView child count

When clicking on RecyclerView list item, RecyclerView#getChildCount()
returns wrong number sometimes. Causing extra divider being drawn at the
list bottom.

Fixes: 170393090
Test: manually
Change-Id: I6f889e99a90510237bfaace2d51da1c389505699
diff --git a/car-ui-lib/src/com/android/car/ui/recyclerview/DefaultScrollBar.java b/car-ui-lib/src/com/android/car/ui/recyclerview/DefaultScrollBar.java
index ec579c3..b3a95d6 100644
--- a/car-ui-lib/src/com/android/car/ui/recyclerview/DefaultScrollBar.java
+++ b/car-ui-lib/src/com/android/car/ui/recyclerview/DefaultScrollBar.java
@@ -313,26 +313,24 @@
      */
     void pageUp() {
         int currentOffset = getRecyclerView().computeVerticalScrollOffset();
-        if (getRecyclerView().getLayoutManager() == null
-                || getRecyclerView().getChildCount() == 0
-                || currentOffset == 0) {
+        RecyclerView.LayoutManager layoutManager = getRecyclerView().getLayoutManager();
+        if (layoutManager == null || layoutManager.getChildCount() == 0 || currentOffset == 0) {
             return;
         }
 
         // Use OrientationHelper to calculate scroll distance in order to match snapping behavior.
-        OrientationHelper orientationHelper =
-                getOrientationHelper(getRecyclerView().getLayoutManager());
+        OrientationHelper orientationHelper = getOrientationHelper(layoutManager);
         int screenSize = orientationHelper.getTotalSpace();
         int scrollDistance = screenSize;
         // The iteration order matters. In case where there are 2 items longer than screen size, we
         // want to focus on upcoming view.
-        for (int i = 0; i < getRecyclerView().getChildCount(); i++) {
+        for (int i = 0; i < layoutManager.getChildCount(); i++) {
             /*
              * We treat child View longer than screen size differently:
              * 1) When it enters screen, next pageUp will align its bottom with parent bottom;
              * 2) When it leaves screen, next pageUp will align its top with parent top.
              */
-            View child = getRecyclerView().getChildAt(i);
+            View child = layoutManager.getChildAt(i);
             if (child.getHeight() > screenSize) {
                 if (orientationHelper.getDecoratedEnd(child) < screenSize) {
                     // Child view bottom is entering screen. Align its bottom with parent bottom.
@@ -361,19 +359,18 @@
      * scrolled the length of a page, but not snapped to.
      */
     void pageDown() {
-        if (getRecyclerView().getLayoutManager() == null
-                || getRecyclerView().getChildCount() == 0) {
+        RecyclerView.LayoutManager layoutManager = getRecyclerView().getLayoutManager();
+        if (layoutManager == null || layoutManager.getChildCount() == 0) {
             return;
         }
 
-        OrientationHelper orientationHelper =
-                getOrientationHelper(getRecyclerView().getLayoutManager());
+        OrientationHelper orientationHelper = getOrientationHelper(layoutManager);
         int screenSize = orientationHelper.getTotalSpace();
         int scrollDistance = screenSize;
 
         // If the last item is partially visible, page down should bring it to the top.
-        View lastChild = getRecyclerView().getChildAt(getRecyclerView().getChildCount() - 1);
-        if (getRecyclerView().getLayoutManager().isViewPartiallyVisible(lastChild,
+        View lastChild = layoutManager.getChildAt(layoutManager.getChildCount() - 1);
+        if (layoutManager.isViewPartiallyVisible(lastChild,
                 /* completelyVisible= */ false, /* acceptEndPointInclusion= */ false)) {
             scrollDistance = orientationHelper.getDecoratedStart(lastChild)
                     - orientationHelper.getStartAfterPadding();
@@ -388,12 +385,12 @@
 
         // The iteration order matters. In case where there are 2 items longer than screen size, we
         // want to focus on upcoming view (the one at the bottom of screen).
-        for (int i = getRecyclerView().getChildCount() - 1; i >= 0; i--) {
+        for (int i = layoutManager.getChildCount() - 1; i >= 0; i--) {
             /* We treat child View longer than screen size differently:
              * 1) When it enters screen, next pageDown will align its top with parent top;
              * 2) When it leaves screen, next pageDown will align its bottom with parent bottom.
              */
-            View child = getRecyclerView().getChildAt(i);
+            View child = layoutManager.getChildAt(i);
             if (child.getHeight() > screenSize) {
                 if (orientationHelper.getDecoratedStart(child)
                         - orientationHelper.getStartAfterPadding() > 0) {
diff --git a/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/grid/GridDividerItemDecoration.java b/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/grid/GridDividerItemDecoration.java
index d462b92..d36346e 100644
--- a/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/grid/GridDividerItemDecoration.java
+++ b/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/grid/GridDividerItemDecoration.java
@@ -24,6 +24,8 @@
 
 import com.android.car.ui.R;
 
+import java.util.Objects;
+
 /** Adds interior dividers to a RecyclerView with a GridLayoutManager. */
 public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {
 
@@ -86,7 +88,9 @@
      * @param parent The RecyclerView onto which dividers are being added
      */
     private void drawHorizontalDividers(Canvas canvas, RecyclerView parent) {
-        int childCount = parent.getChildCount();
+        RecyclerView.LayoutManager layoutManager = Objects.requireNonNull(
+                parent.getLayoutManager());
+        int childCount = layoutManager.getChildCount();
         int rowCount = childCount / mNumColumns;
         int lastRowChildCount = childCount % mNumColumns;
         int lastColumn = Math.min(childCount, mNumColumns);
@@ -99,8 +103,9 @@
                 lastRowChildIndex = i + ((rowCount - 1) * mNumColumns);
             }
 
-            View firstRowChild = parent.getChildAt(i);
-            View lastRowChild = parent.getChildAt(lastRowChildIndex);
+
+            View firstRowChild = layoutManager.getChildAt(i);
+            View lastRowChild = layoutManager.getChildAt(lastRowChildIndex);
 
             int dividerTop =
                     firstRowChild.getTop() + (int) parent.getContext().getResources().getDimension(
@@ -127,7 +132,9 @@
      * @param parent The RecyclerView onto which dividers are being added
      */
     private void drawVerticalDividers(Canvas canvas, RecyclerView parent) {
-        double childCount = parent.getChildCount();
+        RecyclerView.LayoutManager layoutManager = Objects.requireNonNull(
+                parent.getLayoutManager());
+        double childCount = layoutManager.getChildCount();
         double rowCount = Math.ceil(childCount / mNumColumns);
         int rightmostChildIndex;
         for (int i = 1; i <= rowCount; i++) {
@@ -141,8 +148,8 @@
                 rightmostChildIndex = (i * mNumColumns) - 1;
             }
 
-            View leftmostChild = parent.getChildAt(mNumColumns * (i - 1));
-            View rightmostChild = parent.getChildAt(rightmostChildIndex);
+            View leftmostChild = layoutManager.getChildAt(mNumColumns * (i - 1));
+            View rightmostChild = layoutManager.getChildAt(rightmostChildIndex);
 
             // draws on top of each row.
             int dividerLeft =
diff --git a/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/linear/LinearDividerItemDecoration.java b/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/linear/LinearDividerItemDecoration.java
index 0adbdeb..e79468f 100644
--- a/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/linear/LinearDividerItemDecoration.java
+++ b/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/linear/LinearDividerItemDecoration.java
@@ -98,9 +98,10 @@
                 - (int) parent.getContext().getResources().getDimension(
                 R.dimen.car_ui_recyclerview_divider_bottom_margin);
 
-        int childCount = parent.getChildCount();
+        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
+        int childCount = layoutManager.getChildCount();
         for (int i = 0; i < childCount - 1; i++) {
-            View child = parent.getChildAt(i);
+            View child = layoutManager.getChildAt(i);
 
             RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
 
@@ -127,9 +128,10 @@
                 - (int) parent.getContext().getResources().getDimension(
                 R.dimen.car_ui_recyclerview_divider_end_margin);
 
-        int childCount = parent.getChildCount();
+        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
+        int childCount = layoutManager.getChildCount();
         for (int i = 0; i < childCount - 1; i++) {
-            View child = parent.getChildAt(i);
+            View child = layoutManager.getChildAt(i);
 
             RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
 
diff --git a/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/linear/LinearOffsetItemDecoration.java b/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/linear/LinearOffsetItemDecoration.java
index d757f05..37ed388 100644
--- a/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/linear/LinearOffsetItemDecoration.java
+++ b/car-ui-lib/src/com/android/car/ui/recyclerview/decorations/linear/LinearOffsetItemDecoration.java
@@ -164,7 +164,8 @@
             parentLeft = parent.getPaddingLeft();
             offsetDrawableRight = parentLeft + mOffsetDrawable.getIntrinsicWidth();
         } else {
-            View lastChild = parent.getChildAt(parent.getChildCount() - 1);
+            RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
+            View lastChild = layoutManager.getChildAt(layoutManager.getChildCount() - 1);
             RecyclerView.LayoutParams lastChildLayoutParams =
                     (RecyclerView.LayoutParams) lastChild.getLayoutParams();
             parentLeft = lastChild.getRight() + lastChildLayoutParams.rightMargin;
@@ -186,7 +187,8 @@
             parentTop = parent.getPaddingTop();
             offsetDrawableBottom = parentTop + mOffsetDrawable.getIntrinsicHeight();
         } else {
-            View lastChild = parent.getChildAt(parent.getChildCount() - 1);
+            RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
+            View lastChild = layoutManager.getChildAt(layoutManager.getChildCount() - 1);
             RecyclerView.LayoutParams lastChildLayoutParams =
                     (RecyclerView.LayoutParams) lastChild.getLayoutParams();
             parentTop = lastChild.getBottom() + lastChildLayoutParams.bottomMargin;