Updated recycler view demo with smooth scrolling

Change-Id: I039d10a0e707e05dd4c5ce37344f4ecc80d00880
diff --git a/samples/Support7Demos/res/layout/activity_linear_layout_manager.xml b/samples/Support7Demos/res/layout/activity_linear_layout_manager.xml
index f4afe95..d671c73 100644
--- a/samples/Support7Demos/res/layout/activity_linear_layout_manager.xml
+++ b/samples/Support7Demos/res/layout/activity_linear_layout_manager.xml
@@ -17,15 +17,34 @@
               android:orientation="vertical"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
-    <Spinner
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/spinner"/>
+    <LinearLayout
+            android:orientation="horizontal"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content">
+        <EditText
+                android:id="@+id/scroll_offset"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:inputType="numberSigned"
+                android:hint="offset"/>
+        <Spinner
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:id="@+id/spinner"/>
+        <CheckBox
+                android:id="@+id/enable_smooth_scroll"
+                android:text="smooth scroll"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"/>
+    </LinearLayout>
+
     <android.support.v7.widget.RecyclerView
             android:layout_width="fill_parent"
-            android:layout_height="100dp"
+            android:layout_height="60dp"
+            android:scrollbars="horizontal"
             android:id="@+id/config_recycler_view"/>
     <android.support.v7.widget.RecyclerView
+            android:background="#ff0099cc"
             android:scrollbarStyle="insideOverlay"
             android:scrollbars="horizontal|vertical"
             android:layout_width="fill_parent"
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 eb49cff..8c2caa9 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerActivity.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/LinearLayoutManagerActivity.java
@@ -25,16 +25,13 @@
 import android.support.v4.view.ViewCompat;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
-import android.util.DisplayMetrics;
-import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
-import android.widget.ArrayAdapter;
 import android.widget.BaseAdapter;
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
-import android.widget.LinearLayout;
+import android.widget.EditText;
 import android.widget.Spinner;
 import android.widget.TextView;
 
@@ -45,7 +42,9 @@
  */
 public class LinearLayoutManagerActivity extends Activity {
 
-    private LinearLayoutManager mListLayoutManager;
+    private RecyclerView.LayoutManager mListLayoutManager;
+
+    private ListWrapper mListWrapper;
 
     private RecyclerView mRecyclerView;
 
@@ -67,9 +66,68 @@
         mRecyclerView.setHasFixedSize(true);
         mListLayoutManager = new LinearLayoutManager(this);
         mRecyclerView.setLayoutManager(mListLayoutManager);
-        mRecyclerView.setAdapter(new SimpleStringAdapter(this, Cheeses.sCheeseStrings));
-        mDividerItemDecoration = new DividerItemDecoration(this,
-                mListLayoutManager.getOrientation());
+        mRecyclerView.setAdapter(new SimpleStringAdapter(this, Cheeses.sCheeseStrings) {
+            @Override
+            public SimpleStringAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
+                    int viewType) {
+                final SimpleStringAdapter.ViewHolder vh = super
+                        .onCreateViewHolder(parent, viewType);
+                vh.itemView.setOnClickListener(new View.OnClickListener() {
+                    @Override
+                    public void onClick(View v) {
+                        final int pos = vh.getPosition();
+                        if (pos + 1 < getItemCount()) {
+                            swap(pos, pos + 1);
+                        }
+                        notifyItemChanged(pos);
+                    }
+                });
+                return vh;
+            }
+        });
+        initListForLayoutManager();
+    }
+
+    private void initListForLayoutManager() {
+        mListWrapper = new ListWrapper() {
+            @Override
+            public int getOrientation() {
+                return lm().getOrientation();
+            }
+
+            @Override
+            public void setOrientation(int orientation) {
+                lm().setOrientation(orientation);
+            }
+
+            @Override
+            public boolean getReverseLayout() {
+                return lm().getReverseLayout();
+            }
+
+            @Override
+            public void setReverseLayout(boolean newValue) {
+                lm().setReverseLayout(newValue);
+            }
+
+            @Override
+            public boolean getStackFromEnd() {
+                return lm().getStackFromEnd();
+            }
+
+            @Override
+            public void setStackFromEnd(boolean newValue) {
+                lm().setStackFromEnd(newValue);
+            }
+
+            private LinearLayoutManager lm() {
+                return (LinearLayoutManager) mListLayoutManager;
+            }
+        };
+        if (mDividerItemDecoration != null) {
+            mRecyclerView.removeItemDecoration(mDividerItemDecoration);
+        }
+        mDividerItemDecoration = new DividerItemDecoration(this, mListWrapper.getOrientation());
         mRecyclerView.addItemDecoration(mDividerItemDecoration);
     }
 
@@ -83,7 +141,11 @@
     }
 
     private void initSpinner() {
-        Spinner spinner = (Spinner) findViewById(R.id.spinner);
+        final CheckBox checkBox = (CheckBox)
+                findViewById(R.id.enable_smooth_scroll);
+
+        final Spinner spinner = (Spinner) findViewById(R.id.spinner);
+        final EditText scrollOffset = (EditText) findViewById(R.id.scroll_offset);
         spinner.setAdapter(new BaseAdapter() {
             @Override
             public int getCount() {
@@ -102,7 +164,7 @@
 
             @Override
             public View getView(int position, View convertView, ViewGroup parent) {
-                if(convertView == null) {
+                if (convertView == null) {
                     convertView = new TextView(parent.getContext());
                 }
                 ((TextView) convertView).setText("" + position);
@@ -112,7 +174,25 @@
         spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
             @Override
             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
-                mRecyclerView.scrollToPosition(position);
+                int offset = Integer.MIN_VALUE;
+                String offsetString = scrollOffset.getText().toString();
+                try {
+                    offset = Integer.parseInt(offsetString);
+                } catch (NumberFormatException ex) {
+
+                }
+
+                if (offset == Integer.MIN_VALUE) {
+                    if (checkBox.isChecked()) {
+                        mRecyclerView.smoothScrollToPosition(position);
+                    } else {
+                        mRecyclerView.scrollToPosition(position);
+                    }
+                } else {
+                    // ignore offset until we add recycling list view with smooth scroll to offset
+                    mRecyclerView.smoothScrollToPosition(position);
+                }
+
             }
 
             @Override
@@ -127,25 +207,28 @@
                 new ConfigToggle(R.string.checkbox_orientation) {
                     @Override
                     public boolean isChecked() {
-                        return mListLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL;
+                        return mListWrapper.getOrientation() == LinearLayoutManager.HORIZONTAL;
                     }
 
                     @Override
                     public void onChange(boolean newValue) {
-                        mListLayoutManager.setOrientation(newValue ? LinearLayoutManager.HORIZONTAL
+                        mListWrapper.setOrientation(newValue ? LinearLayoutManager.HORIZONTAL
                                 : LinearLayoutManager.VERTICAL);
-                        mDividerItemDecoration.setOrientation(mListLayoutManager.getOrientation());
+                        if (mDividerItemDecoration != null) {
+                            mDividerItemDecoration.setOrientation(mListWrapper.getOrientation());
+                        }
+
                     }
                 },
                 new ConfigToggle(R.string.checkbox_reverse) {
                     @Override
                     public boolean isChecked() {
-                        return mListLayoutManager.getReverseLayout();
+                        return mListWrapper.getReverseLayout();
                     }
 
                     @Override
                     public void onChange(boolean newValue) {
-                        mListLayoutManager.setReverseLayout(newValue);
+                        mListWrapper.setReverseLayout(newValue);
                     }
                 },
                 new ConfigToggle(R.string.checkbox_layout_dir) {
@@ -164,12 +247,12 @@
                 new ConfigToggle(R.string.checkbox_stack_from_end) {
                     @Override
                     public boolean isChecked() {
-                        return mListLayoutManager.getStackFromEnd();
+                        return mListWrapper.getStackFromEnd();
                     }
 
                     @Override
                     public void onChange(boolean newValue) {
-                        mListLayoutManager.setStackFromEnd(newValue);
+                        mListWrapper.setStackFromEnd(newValue);
                     }
                 }
         };
@@ -221,7 +304,6 @@
     }
 
 
-
     private RecyclerView.Adapter mConfigAdapter = new RecyclerView.Adapter<ConfigViewHolder>() {
         @Override
         public ConfigViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
@@ -240,4 +322,25 @@
         }
     };
 
+
+    /**
+     * To avoid adding interfaces to LayoutManager for the demo, we use this wrapper class to
+     * call different LayoutManagers
+     */
+    private static interface ListWrapper {
+
+        int getOrientation();
+
+        void setOrientation(int orientation);
+
+        boolean getReverseLayout();
+
+        void setReverseLayout(boolean newValue);
+
+        boolean getStackFromEnd();
+
+        void setStackFromEnd(boolean newValue);
+    }
+
+
 }
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/widget/adapter/SimpleStringAdapter.java b/samples/Support7Demos/src/com/example/android/supportv7/widget/adapter/SimpleStringAdapter.java
index 110eb62..a12e79d 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/widget/adapter/SimpleStringAdapter.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/widget/adapter/SimpleStringAdapter.java
@@ -17,9 +17,9 @@
 package com.example.android.supportv7.widget.adapter;
 
 import android.content.Context;
+import android.graphics.Color;
 import android.support.v7.widget.RecyclerView;
 import android.util.TypedValue;
-import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 
@@ -34,16 +34,16 @@
 
     public static class ViewHolder extends RecyclerView.ViewHolder {
 
-        public TextView textView;
+        public TextView mTextView;
 
         public ViewHolder(TextView v) {
             super(v);
-            textView = v;
+            mTextView = v;
         }
 
         @Override
         public String toString() {
-            return super.toString() + " '" + textView.getText();
+            return super.toString() + " '" + mTextView.getText();
         }
     }
 
@@ -69,16 +69,36 @@
     @Override
     public SimpleStringAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         final ViewHolder h = new ViewHolder(new TextView(parent.getContext()));
-        h.textView.setMinimumHeight(128);
-        h.textView.setPadding(20, 0, 20, 0);
-        h.textView.setFocusable(true);
-        h.textView.setBackgroundResource(mBackground);
+        h.mTextView.setMinimumHeight(128);
+        h.mTextView.setPadding(20, 0, 20, 0);
+        h.mTextView.setFocusable(true);
+        h.mTextView.setBackgroundResource(mBackground);
+        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
+                ViewGroup.LayoutParams.WRAP_CONTENT,
+                ViewGroup.LayoutParams.WRAP_CONTENT);
+        lp.leftMargin = 10;
+        lp.rightMargin = 5;
+        lp.topMargin = 20;
+        lp.bottomMargin = 15;
+        h.mTextView.setLayoutParams(lp);
         return h;
     }
 
     @Override
     public void onBindViewHolder(ViewHolder holder, int position) {
-        holder.textView.setText(position + ":" + mValues.get(position));
+        holder.mTextView.setText(position + ":" + mValues.get(position));
+        holder.mTextView.setMinHeight((200 + mValues.get(position).length() * 10));
+        holder.mTextView.setBackgroundColor(getBackgroundColor(position));
+    }
+
+    private int getBackgroundColor(int position) {
+        switch (position % 4) {
+            case 0: return Color.BLACK;
+            case 1: return Color.RED;
+            case 2: return Color.GREEN;
+            case 3: return Color.BLUE;
+        }
+        return Color.TRANSPARENT;
     }
 
     @Override