Merge changes from topic "am-8682d6a2-aa7f-4a73-a8d6-f2ead69fc0aa"

* changes:
  [automerger] Avoid carried-over sub-view layout params. am: a565726ba2
  Avoid carried-over sub-view layout params.
diff --git a/car/src/main/java/androidx/car/widget/ListItem.java b/car/src/main/java/androidx/car/widget/ListItem.java
index c5b93d9..ad1e3b8 100644
--- a/car/src/main/java/androidx/car/widget/ListItem.java
+++ b/car/src/main/java/androidx/car/widget/ListItem.java
@@ -276,6 +276,8 @@
          * margin otherwise align center vertically.
          */
         private void setPrimaryIconLayout() {
+            // Set all relevant fields in layout params to avoid carried over params when the item
+            // gets bound to a recycled view holder.
             switch (mPrimaryActionType) {
                 case PRIMARY_ACTION_TYPE_SMALL_ICON:
                     mBinders.add(vh -> {
@@ -293,11 +295,13 @@
 
                         if (!TextUtils.isEmpty(mBody)) {
                             // Set top margin.
+                            layoutParams.removeRule(RelativeLayout.CENTER_VERTICAL);
                             layoutParams.topMargin = mContext.getResources().getDimensionPixelSize(
                                     R.dimen.car_padding_4);
                         } else {
                             // Centered vertically.
                             layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
+                            layoutParams.topMargin = 0;
                         }
                         vh.getPrimaryIcon().setLayoutParams(layoutParams);
                     });
@@ -317,6 +321,7 @@
 
                         // Always centered vertically.
                         layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
+                        layoutParams.topMargin = 0;
 
                         vh.getPrimaryIcon().setLayoutParams(layoutParams);
                     });
@@ -396,12 +401,15 @@
          * Sets top/bottom margins of {@code Title} and {@code Body}.
          */
         private void setTextVerticalMargin() {
+            // Set all relevant fields in layout params to avoid carried over params when the item
+            // gets bound to a recycled view holder.
             if (!TextUtils.isEmpty(mTitle) && TextUtils.isEmpty(mBody)) {
                 // Title only - view is aligned center vertically by itself.
                 mBinders.add(vh -> {
                     RelativeLayout.LayoutParams layoutParams =
                             (RelativeLayout.LayoutParams) vh.getTitle().getLayoutParams();
                     layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
+                    layoutParams.topMargin = 0;
                     vh.getTitle().setLayoutParams(layoutParams);
                 });
             } else if (TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mBody)) {
@@ -412,6 +420,7 @@
                     RelativeLayout.LayoutParams layoutParams =
                             (RelativeLayout.LayoutParams) vh.getBody().getLayoutParams();
                     layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
+                    layoutParams.removeRule(RelativeLayout.BELOW);
                     layoutParams.topMargin = margin;
                     layoutParams.bottomMargin = margin;
                     vh.getBody().setLayoutParams(layoutParams);
@@ -422,13 +431,16 @@
                     Resources resources = mContext.getResources();
                     int padding1 = resources.getDimensionPixelSize(R.dimen.car_padding_1);
                     int padding3 = resources.getDimensionPixelSize(R.dimen.car_padding_3);
+
                     RelativeLayout.LayoutParams titleLayoutParams =
                             (RelativeLayout.LayoutParams) vh.getTitle().getLayoutParams();
+                    titleLayoutParams.removeRule(RelativeLayout.CENTER_VERTICAL);
                     titleLayoutParams.topMargin = padding3;
                     vh.getTitle().setLayoutParams(titleLayoutParams);
                     // Body is below title with a margin, and has bottom margin.
                     RelativeLayout.LayoutParams bodyLayoutParams =
                             (RelativeLayout.LayoutParams) vh.getBody().getLayoutParams();
+                    bodyLayoutParams.removeRule(RelativeLayout.CENTER_VERTICAL);
                     bodyLayoutParams.addRule(RelativeLayout.BELOW, R.id.title);
                     bodyLayoutParams.topMargin = padding1;
                     bodyLayoutParams.bottomMargin = padding3;
diff --git a/car/tests/src/androidx/car/widget/ListItemTest.java b/car/tests/src/androidx/car/widget/ListItemTest.java
index 2b974c3..64b9ede 100644
--- a/car/tests/src/androidx/car/widget/ListItemTest.java
+++ b/car/tests/src/androidx/car/widget/ListItemTest.java
@@ -42,6 +42,7 @@
 import android.support.v7.widget.CardView;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.RelativeLayout;
 
 import org.hamcrest.Matcher;
 import org.junit.Before;
@@ -497,6 +498,46 @@
                 is(equalTo(bodyPrimary.getBody().getTextColors())));
     }
 
+    @Test
+    public void testNoCarriedOverLayoutParamsForTextView() throws Throwable {
+        ListItem singleLine = new ListItem.Builder(mActivity).withTitle("t").build();
+        setupPagedListView(Arrays.asList(singleLine));
+
+        // Manually rebind the view holder of a single line item to a double line item.
+        ListItem doubleLine = new ListItem.Builder(mActivity).withTitle("t").withBody("b").build();
+        ListItemAdapter.ViewHolder viewHolder = getViewHolderAtPosition(0);
+        mActivityRule.runOnUiThread(() -> doubleLine.bind(viewHolder));
+
+        RelativeLayout.LayoutParams titleLayoutParams =
+                (RelativeLayout.LayoutParams) viewHolder.getTitle().getLayoutParams();
+        RelativeLayout.LayoutParams bodyLayoutParams =
+                (RelativeLayout.LayoutParams) viewHolder.getTitle().getLayoutParams();
+        assertThat(titleLayoutParams.getRule(RelativeLayout.CENTER_VERTICAL), is(equalTo(0)));
+        assertThat(bodyLayoutParams.getRule(RelativeLayout.CENTER_VERTICAL), is(equalTo(0)));
+    }
+
+    @Test
+    public void testNoCarriedOverLayoutParamsForPrimaryIcon() throws Throwable {
+        ListItem smallIcon = new ListItem.Builder(mActivity)
+                .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, false)
+                .withBody("body")  // Small icon of items with body text should use top margin.
+                .build();
+        setupPagedListView(Arrays.asList(smallIcon));
+
+        // Manually rebind the view holder.
+        ListItem largeIcon = new ListItem.Builder(mActivity)
+                .withPrimaryActionIcon(android.R.drawable.sym_def_app_icon, true)
+                .build();
+        ListItemAdapter.ViewHolder viewHolder = getViewHolderAtPosition(0);
+        mActivityRule.runOnUiThread(() -> largeIcon.bind(viewHolder));
+
+        RelativeLayout.LayoutParams iconLayoutParams =
+                (RelativeLayout.LayoutParams) viewHolder.getPrimaryIcon().getLayoutParams();
+        assertThat(iconLayoutParams.getRule(RelativeLayout.CENTER_VERTICAL),
+                is(equalTo(RelativeLayout.TRUE)));
+        assertThat(iconLayoutParams.topMargin, is(equalTo(0)));
+    }
+
     private static ViewAction clickChildViewWithId(final int id) {
         return new ViewAction() {
             @Override