Read clip to padding from attrs

GetClipToPadding is API 21 so RecyclerView steals that
vlaue by overriding setClipToPadding and tracking the
value itself.

Unfortunately, when clipToPadding is defined in the XML and set
by the parent's constructor, RV's field initialization was overriding
the value.
Apperantly, the R.attr.clipToPadding is from API 1
so we can use it to read the value (getter is from 21).

Bug: 29123702
Bug: 26779229
Change-Id: I0521413bbf81c6c5428e7305eefa838c988fc018
diff --git a/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java b/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
index cd318e8..20f8ae7 100644
--- a/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
+++ b/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
@@ -157,6 +157,8 @@
     private static final int[]  NESTED_SCROLLING_ATTRS
             = {16843830 /* android.R.attr.nestedScrollingEnabled */};
 
+    private static final int[] CLIP_TO_PADDING_ATTR = {android.R.attr.clipToPadding};
+
     /**
      * On Kitkat and JB MR2, there is a bug which prevents DisplayList from being invalidated if
      * a View is two levels deep(wrt to ViewHolder.itemView). DisplayList can be invalidated by
@@ -277,7 +279,7 @@
      * Prior to L, there is no way to query this variable which is why we override the setter and
      * track it here.
      */
-    private boolean mClipToPadding = true;
+    private boolean mClipToPadding;
 
     /**
      * Note: this Runnable is only ever posted if:
@@ -479,6 +481,13 @@
 
     public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
+        if (attrs != null) {
+            TypedArray a = context.obtainStyledAttributes(attrs, CLIP_TO_PADDING_ATTR, defStyle, 0);
+            mClipToPadding = a.getBoolean(0, true);
+            a.recycle();
+        } else {
+            mClipToPadding = true;
+        }
         setScrollContainer(true);
         setFocusableInTouchMode(true);
         final int version = Build.VERSION.SDK_INT;
diff --git a/v7/recyclerview/tests/res/layout/inflation_test.xml b/v7/recyclerview/tests/res/layout/inflation_test.xml
index 91d900f..b34a1a3 100644
--- a/v7/recyclerview/tests/res/layout/inflation_test.xml
+++ b/v7/recyclerview/tests/res/layout/inflation_test.xml
@@ -15,69 +15,92 @@
   -->
 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:app="http://schemas.android.com/apk/res-auto"
-    android:orientation="vertical"
-    android:layout_width="fill_parent"
-    android:layout_height="fill_parent">
+              xmlns:app="http://schemas.android.com/apk/res-auto"
+              android:orientation="vertical"
+              android:layout_width="fill_parent"
+              android:layout_height="fill_parent">
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/recyclerView"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp"
-        app:layoutManager="GridLayoutManager"
-        android:orientation="horizontal"
-        app:spanCount="3"
-        app:reverseLayout="true" />
+            android:id="@+id/clipToPaddingNo"
+            android:layout_width="fill_parent"
+            app:layoutManager="LinearLayoutManager"
+            android:clipToPadding="false"
+            android:layout_height="100dp"/>
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/recyclerView2"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp"
-        app:layoutManager=".CustomLayoutManager"
-        android:orientation="vertical"
-        app:stackFromEnd="true" />
+            android:id="@+id/clipToPaddingUndefined"
+            android:layout_width="fill_parent"
+            app:layoutManager="LinearLayoutManager"
+            android:layout_height="100dp"/>
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/recyclerView3"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp"
-        app:layoutManager=".CustomLayoutManager$LayoutManager" />
+            android:id="@+id/clipToPaddingYes"
+            android:layout_width="fill_parent"
+            app:layoutManager="LinearLayoutManager"
+            android:clipToPadding="true"
+            android:layout_height="100dp"/>
+
+    <android.support.v7.widget.RecyclerView
+            android:id="@+id/recyclerView"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"
+            app:layoutManager="GridLayoutManager"
+            android:orientation="horizontal"
+            app:spanCount="3"
+            app:reverseLayout="true"/>
+
+    <android.support.v7.widget.RecyclerView
+            android:id="@+id/recyclerView2"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"
+            app:layoutManager=".CustomLayoutManager"
+            android:orientation="vertical"
+            app:stackFromEnd="true"/>
+
+    <android.support.v7.widget.RecyclerView
+            android:id="@+id/recyclerView3"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"
+            app:layoutManager=".CustomLayoutManager$LayoutManager"/>
 
     <android.support.v7.widget.RecyclerView
             android:id="@+id/recyclerView4"
             android:layout_width="fill_parent"
             android:layout_height="100dp"
-            app:layoutManager=".PrivateLayoutManager" />
+            app:layoutManager=".PrivateLayoutManager"/>
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/recyclerView5"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp" />
+            android:id="@+id/recyclerView5"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"/>
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/recyclerView6"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp"
-        android:nestedScrollingEnabled="false" />
+            android:id="@+id/recyclerView6"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"
+            android:nestedScrollingEnabled="false"/>
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/focusability_undefined"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp"/>
+            android:id="@+id/focusability_undefined"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"/>
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/focusability_after"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp"
-        android:descendantFocusability="afterDescendants"/>
+            android:id="@+id/focusability_after"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"
+            android:descendantFocusability="afterDescendants"/>
+
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/focusability_before"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp"
-        android:descendantFocusability="beforeDescendants"/>
+            android:id="@+id/focusability_before"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"
+            android:descendantFocusability="beforeDescendants"/>
+
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/focusability_block"
-        android:layout_width="fill_parent"
-        android:layout_height="100dp"
-        android:descendantFocusability="blocksDescendants"/>
+            android:id="@+id/focusability_block"
+            android:layout_width="fill_parent"
+            android:layout_height="100dp"
+            android:descendantFocusability="blocksDescendants"/>
+
 </LinearLayout>
\ No newline at end of file
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/test/RecyclerViewTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/test/RecyclerViewTest.java
index ee9e380..934936b 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/test/RecyclerViewTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/test/RecyclerViewTest.java
@@ -72,7 +72,15 @@
     public void inflation() throws Throwable {
         setContentView(R.layout.inflation_test);
         getInstrumentation().waitForIdleSync();
-        RecyclerView view = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
+        RecyclerView view;
+        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingUndefined);
+        assertTrue(view.getLayoutManager().getClipToPadding());
+        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingYes);
+        assertTrue(view.getLayoutManager().getClipToPadding());
+        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingNo);
+        assertFalse(view.getLayoutManager().getClipToPadding());
+
+        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
         RecyclerView.LayoutManager layoutManager = view.getLayoutManager();
         assertNotNull("LayoutManager not created.", layoutManager);
         assertEquals("Incorrect LayoutManager created",