Simple Nested RecyclerView sample

bug:27106058

Change-Id: I4aabe4eb425e258e33116c555a73d609d7a4c8d9
diff --git a/samples/Support7Demos/AndroidManifest.xml b/samples/Support7Demos/AndroidManifest.xml
index a26d7fb..aac5a6d 100644
--- a/samples/Support7Demos/AndroidManifest.xml
+++ b/samples/Support7Demos/AndroidManifest.xml
@@ -379,9 +379,18 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".widget.NestedRecyclerViewActivity"
+                  android:label="@string/nested_recycler_view"
+                  android:theme="@style/Theme.AppCompat">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.example.android.supportv7.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".widget.LinearLayoutManagerActivity"
-            android:label="@string/linear_layout_manager"
-            android:theme="@style/Theme.AppCompat">
+                  android:label="@string/linear_layout_manager"
+                  android:theme="@style/Theme.AppCompat">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="com.example.android.supportv7.SAMPLE_CODE" />
@@ -389,8 +398,8 @@
         </activity>
 
         <activity android:name=".widget.LinearLayoutManagerJankActivity"
-            android:label="@string/linear_layout_manager"
-            android:theme="@style/Theme.AppCompat">
+                  android:label="@string/linear_layout_manager_jank"
+                  android:theme="@style/Theme.AppCompat">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="com.example.android.supportv7.SAMPLE_CODE" />
diff --git a/samples/Support7Demos/res/layout/nested_item.xml b/samples/Support7Demos/res/layout/nested_item.xml
new file mode 100644
index 0000000..cc36ec4
--- /dev/null
+++ b/samples/Support7Demos/res/layout/nested_item.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="wrap_content"
+    android:layout_height="100dp"
+    android:minWidth="100dp"
+    android:gravity="center"/>
\ No newline at end of file
diff --git a/samples/Support7Demos/res/values/strings.xml b/samples/Support7Demos/res/values/strings.xml
index ad87a6a..afb37b0 100644
--- a/samples/Support7Demos/res/values/strings.xml
+++ b/samples/Support7Demos/res/values/strings.xml
@@ -132,8 +132,10 @@
     <string name="sample_media_route_activity_presentation">Local Playback on Presentation Display</string>
 
     <string name="recycler_view">RecyclerView/RecyclerViewActivity</string>
-    <string name="animated_recycler_view">RecyclerView/AnimatedRecyclerView</string>
+    <string name="animated_recycler_view">RecyclerView/Animated RecyclerView</string>
+    <string name="nested_recycler_view">RecyclerView/Nested RecyclerView</string>
     <string name="linear_layout_manager">RecyclerView/Linear Layout Manager</string>
+    <string name="linear_layout_manager_jank">RecyclerView/Janky Linear Layout Manager</string>
     <string name="grid_layout_manager">RecyclerView/Grid Layout Manager</string>
     <string name="staggered_grid_layout_manager">RecyclerView/Staggered Grid Layout Manager</string>
     <string name="async_list_util">RecyclerView/AsyncListUtil</string>
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/widget/BaseLayoutManagerActivity.java b/samples/Support7Demos/src/com/example/android/supportv7/widget/BaseLayoutManagerActivity.java
index 002a574..a4c01d1 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/widget/BaseLayoutManagerActivity.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/BaseLayoutManagerActivity.java
@@ -169,7 +169,9 @@
         scrollToPosition(smooth, position);
     }
 
-    abstract ConfigToggle[] createConfigToggles();
+    protected ConfigToggle[] createConfigToggles() {
+        return new ConfigToggle[] {};
+    }
 
     private RecyclerView.Adapter mConfigAdapter = new RecyclerView.Adapter<ConfigViewHolder>() {
         @Override
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/widget/GridLayoutManagerActivity.java b/samples/Support7Demos/src/com/example/android/supportv7/widget/GridLayoutManagerActivity.java
index 3ce5c60..1191be0 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/widget/GridLayoutManagerActivity.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/GridLayoutManagerActivity.java
@@ -49,7 +49,7 @@
     };
 
     @Override
-    ConfigToggle[] createConfigToggles() {
+    protected ConfigToggle[] createConfigToggles() {
         return new ConfigToggle[]{
                 new ConfigToggle(this, R.string.checkbox_orientation) {
                     @Override
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerActivity.java b/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerActivity.java
index 914f1e8..31d53cc 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerActivity.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerActivity.java
@@ -42,7 +42,7 @@
     }
 
     @Override
-    ConfigToggle[] createConfigToggles() {
+    protected ConfigToggle[] createConfigToggles() {
         return new ConfigToggle[]{
                 new ConfigToggle(this, R.string.checkbox_orientation) {
                     @Override
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerJankActivity.java b/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerJankActivity.java
index 1709d3e..a9743d3 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerJankActivity.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerJankActivity.java
@@ -60,7 +60,7 @@
     }
 
     @Override
-    ConfigToggle[] createConfigToggles() {
+    protected ConfigToggle[] createConfigToggles() {
         return new ConfigToggle[]{
                 new ConfigToggle(this, R.string.enable_bind_slowdown) {
                     @Override
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/widget/NestedRecyclerViewActivity.java b/samples/Support7Demos/src/com/example/android/supportv7/widget/NestedRecyclerViewActivity.java
new file mode 100644
index 0000000..a9caf40
--- /dev/null
+++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/NestedRecyclerViewActivity.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.supportv7.widget;
+
+import android.graphics.Color;
+import android.support.v7.widget.DividerItemDecoration;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import android.view.LayoutInflater;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import com.example.android.supportv7.Cheeses;
+import com.example.android.supportv7.R;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+/**
+ * A sample nested RecyclerView activity.
+ */
+public class NestedRecyclerViewActivity extends BaseLayoutManagerActivity<LinearLayoutManager> {
+    @Override
+    protected LinearLayoutManager createLayoutManager() {
+        return new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
+    }
+
+    @Override
+    protected void onRecyclerViewInit(RecyclerView recyclerView) {
+        recyclerView.addItemDecoration(
+                new DividerItemDecoration(this, mLayoutManager.getOrientation()));
+    }
+
+    static class InnerAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder> {
+        public static class ViewHolder extends RecyclerView.ViewHolder {
+            public TextView mTextView;
+
+            public ViewHolder(TextView itemView) {
+                super(itemView);
+                mTextView = itemView;
+            }
+        }
+
+        private String[] mData;
+
+        public InnerAdapter(String[] data) {
+            mData = data;
+        }
+
+        @Override
+        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
+            TextView textView = (TextView) inflater.inflate(R.layout.nested_item, parent, false);
+            textView.setMinimumWidth(300);
+            textView.setMinimumHeight(300);
+            return new ViewHolder(textView);
+        }
+
+        @Override
+        public void onBindViewHolder(ViewHolder holder, int position) {
+            holder.mTextView.setText(mData[position]);
+            boolean even = position % 2 == 0;
+            holder.mTextView.setBackgroundColor(even ? Color.TRANSPARENT : 0x2fffffff);
+        }
+
+        @Override
+        public int getItemCount() {
+            return mData.length;
+        }
+    }
+
+    static class OuterAdapter extends RecyclerView.Adapter<OuterAdapter.ViewHolder> {
+        public static class ViewHolder extends RecyclerView.ViewHolder {
+            private final RecyclerView mRecyclerView;
+            public ViewHolder(RecyclerView itemView) {
+                super(itemView);
+                mRecyclerView = itemView;
+            }
+        }
+
+        ArrayList<InnerAdapter> mAdapters = new ArrayList<>();
+        RecyclerView.RecycledViewPool mSharedPool = new RecyclerView.RecycledViewPool();
+
+        public OuterAdapter(String[] data) {
+            int currentCharIndex = 0;
+            char currentChar = data[0].charAt(0);
+            for (int i = 1; i <= data.length; i++) {
+                if (i == data.length || data[i].charAt(0) != currentChar) {
+                    mAdapters.add(new InnerAdapter(
+                            Arrays.copyOfRange(data, currentCharIndex, i - 1)));
+                    if (i < data.length) {
+                        currentChar = data[i].charAt(0);
+                        currentCharIndex = i;
+                    }
+                }
+            }
+        }
+
+        @Override
+        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+            RecyclerView rv = new RecyclerView(parent.getContext());
+            rv.setLayoutManager(new LinearLayoutManager(parent.getContext(),
+                    LinearLayoutManager.HORIZONTAL, false));
+            rv.setRecycledViewPool(mSharedPool);
+            return new ViewHolder(rv);
+        }
+
+        @Override
+        public void onBindViewHolder(ViewHolder holder, int position) {
+            holder.mRecyclerView.setAdapter(mAdapters.get(position));
+        }
+
+        @Override
+        public int getItemCount() {
+            return mAdapters.size();
+        }
+    }
+
+    protected RecyclerView.Adapter createAdapter() {
+        return new OuterAdapter(Cheeses.sCheeseStrings);
+    }
+}
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/widget/StaggeredGridLayoutManagerActivity.java b/samples/Support7Demos/src/com/example/android/supportv7/widget/StaggeredGridLayoutManagerActivity.java
index 39d8657..80e31d0 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/widget/StaggeredGridLayoutManagerActivity.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/StaggeredGridLayoutManagerActivity.java
@@ -36,7 +36,7 @@
     }
 
     @Override
-    ConfigToggle[] createConfigToggles() {
+    protected ConfigToggle[] createConfigToggles() {
         return new ConfigToggle[] {
                 new ConfigToggle(this, R.string.vertical) {
                     @Override