Add RowsFragment example

Bug: 18622128
Change-Id: I12146a766fa383782f9917352c5959b6064835ee
diff --git a/samples/SupportLeanbackDemos/AndroidManifest.xml b/samples/SupportLeanbackDemos/AndroidManifest.xml
index e9fc37c..7e5f6a6 100644
--- a/samples/SupportLeanbackDemos/AndroidManifest.xml
+++ b/samples/SupportLeanbackDemos/AndroidManifest.xml
@@ -30,6 +30,10 @@
             android:theme="@style/Theme.Example.Leanback.Details"
             android:exported="true" />
 
+        <activity android:name="RowsActivity"
+            android:theme="@style/Theme.Example.Leanback.Rows"
+            android:exported="true" />
+
         <activity android:name="PlaybackOverlayActivity"
             android:exported="true" />
 
diff --git a/samples/SupportLeanbackDemos/res/layout/rows.xml b/samples/SupportLeanbackDemos/res/layout/rows.xml
new file mode 100644
index 0000000..d77f7ca
--- /dev/null
+++ b/samples/SupportLeanbackDemos/res/layout/rows.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2014 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.
+-->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/rows_frame"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+  <TextView
+      android:id="@+id/rows_title"
+      android:text="RowsFragment"
+      style="?attr/browseTitleTextStyle"
+      android:paddingStart="?attr/browsePaddingStart"
+      android:paddingEnd="?attr/browsePaddingEnd"
+      android:paddingTop="?attr/browsePaddingTop"
+      android:paddingBottom="10dp"
+      android:layout_width="match_parent"
+      android:layout_height="wrap_content" />
+  <fragment
+      android:name="com.example.android.leanback.RowsFragment"
+      android:id="@+id/main_rows_fragment"
+      android:layout_width="match_parent"
+      android:layout_height="match_parent"
+  />
+
+</FrameLayout>
diff --git a/samples/SupportLeanbackDemos/res/values/styles.xml b/samples/SupportLeanbackDemos/res/values/styles.xml
new file mode 100644
index 0000000..17ea0ef
--- /dev/null
+++ b/samples/SupportLeanbackDemos/res/values/styles.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2014 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.
+-->
+<resources xmlns:android="http://schemas.android.com/apk/res/android">
+    <style name="Widget.Example.Leanback.Title.Text" parent="Widget.Leanback.Title.Text" >
+    </style>
+    <style name="Widget.Example.Leanback.Rows.VerticalGridView" parent="Widget.Leanback.Rows.VerticalGridView">
+        <item name="android:paddingTop">96dp</item>
+    </style>
+</resources>
\ No newline at end of file
diff --git a/samples/SupportLeanbackDemos/res/values/themes.xml b/samples/SupportLeanbackDemos/res/values/themes.xml
index 25eb47f..22a41f0 100644
--- a/samples/SupportLeanbackDemos/res/values/themes.xml
+++ b/samples/SupportLeanbackDemos/res/values/themes.xml
@@ -1,17 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2014 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.
+-->
 <resources>
     <style name="Theme.Example.Leanback" parent="Theme.Leanback">
-<!-- uncomment to override default transition settings:
-        <item name="android:windowEnterTransition">@android:transition/fade</item>
-        <item name="android:windowExitTransition">@android:transition/fade</item>
-        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
-        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
-        <item name="android:windowAllowExitTransitionOverlap">true</item>
-        <item name="android:windowAllowEnterTransitionOverlap">false</item>
-        <item name="android:windowContentTransitions">true</item>
- -->
     </style>
     <style name="Theme.Example.Leanback.Browse" parent="Theme.Leanback.Browse">
     </style>
     <style name="Theme.Example.Leanback.Details" parent="Theme.Leanback.Details">
     </style>
+    <style name="Theme.Example.Leanback.Rows" parent="Theme.Leanback">
+        <item name="browseTitleTextStyle">@style/Widget.Example.Leanback.Title.Text</item>
+        <item name="rowsVerticalGridStyle">@style/Widget.Example.Leanback.Rows.VerticalGridView</item>
+    </style>
 </resources>
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java
index a73242b..91fec5f 100644
--- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java
@@ -97,8 +97,8 @@
             listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
             listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5));
             listRowAdapter.add(new PhotoItem("This is a test", "Only a test", R.drawable.gallery_photo_6));
-            listRowAdapter.add(new PhotoItem("Android TV", "by Google", R.drawable.gallery_photo_7));
-            listRowAdapter.add(new PhotoItem("Leanback", "click to open MainActivity", R.drawable.gallery_photo_8));
+            listRowAdapter.add(new PhotoItem("Android TV", "open RowsActivity", R.drawable.gallery_photo_7));
+            listRowAdapter.add(new PhotoItem("Leanback", "open MainActivity", R.drawable.gallery_photo_8));
             HeaderItem header = new HeaderItem(i, "Row " + i);
             mRowsAdapter.add(new ListRow(header, listRowAdapter));
         }
@@ -117,6 +117,10 @@
                 intent = new Intent(getActivity(), MainActivity.class);
                 bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
                         .toBundle();
+            } else if ( ((PhotoItem) item).getImageResourceId() == R.drawable.gallery_photo_7) {
+                intent = new Intent(getActivity(), RowsActivity.class);
+                bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity())
+                        .toBundle();
             } else {
                 intent = new Intent(getActivity(), DetailsActivity.class);
                 intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item);
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/RowsActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/RowsActivity.java
new file mode 100644
index 0000000..133e995
--- /dev/null
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/RowsActivity.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2014 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.leanback;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.support.v17.leanback.R;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+public class RowsActivity extends Activity implements RowsFragment.OnRowsFirstLineSelectedListener
+{
+    TextView mTitleView;
+    /** Called when the activity is first created. */
+    @Override
+    public void onCreate(Bundle savedInstanceState)
+    {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.rows);
+        mTitleView = (TextView) findViewById(R.id.rows_title);
+    }
+
+    @Override
+    public void onSelectedFirstRow(boolean firstRow) {
+        mTitleView.setVisibility(firstRow ? View.VISIBLE : View.INVISIBLE);
+    }
+}
diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/RowsFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/RowsFragment.java
new file mode 100644
index 0000000..2509bfc
--- /dev/null
+++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/RowsFragment.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2014 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.leanback;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v4.app.ActivityOptionsCompat;
+import android.support.v17.leanback.R;
+import android.support.v17.leanback.widget.ArrayObjectAdapter;
+import android.support.v17.leanback.widget.HeaderItem;
+import android.support.v17.leanback.widget.ImageCardView;
+import android.support.v17.leanback.widget.ListRow;
+import android.support.v17.leanback.widget.ListRowPresenter;
+import android.support.v17.leanback.widget.OnItemViewClickedListener;
+import android.support.v17.leanback.widget.OnItemViewSelectedListener;
+import android.support.v17.leanback.widget.Presenter;
+import android.support.v17.leanback.widget.Row;
+import android.support.v17.leanback.widget.RowPresenter;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+public class RowsFragment extends android.support.v17.leanback.app.RowsFragment {
+
+    public static interface OnRowsFirstLineSelectedListener {
+        void onSelectedFirstRow(boolean firstRow);
+    }
+
+    private static final String TAG = "leanback.RowsFragment";
+
+    private static final int NUM_ROWS = 10;
+    private ArrayObjectAdapter mRowsAdapter;
+    private OnRowsFirstLineSelectedListener mCallback;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        Log.i(TAG, "onCreate");
+        super.onCreate(savedInstanceState);
+
+        setupRows();
+        setOnItemViewClickedListener(new ItemViewClickedListener());
+        setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
+            @Override
+            public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
+                    RowPresenter.ViewHolder rowViewHolder, Row row) {
+                Log.i(TAG, "onItemSelected: " + item + " row " + row);
+                if (mCallback == null) {
+                    return;
+                }
+                if (mRowsAdapter != null && mRowsAdapter.size() > 0 && row != null &&
+                        row != mRowsAdapter.get(0)) {
+                    mCallback.onSelectedFirstRow(false);
+                } else {
+                    mCallback.onSelectedFirstRow(true);
+                }
+            }
+        });
+    }
+
+    @Override
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+        // This makes sure that the container activity has implemented
+        if (activity instanceof OnRowsFirstLineSelectedListener) {
+            mCallback = (OnRowsFirstLineSelectedListener) activity;
+        }
+    }
+
+    private void setupRows() {
+        ListRowPresenter lrp = new ListRowPresenter();
+        lrp.setRowHeight(CardPresenter.getRowHeight(getActivity()));
+        lrp.setExpandedRowHeight(CardPresenter.getExpandedRowHeight(getActivity()));
+
+        mRowsAdapter = new ArrayObjectAdapter(lrp);
+
+        // For good performance, it's important to use a single instance of
+        // a card presenter for all rows using that presenter.
+        final CardPresenter cardPresenter = new CardPresenter();
+
+        for (int i = 0; i < NUM_ROWS; ++i) {
+            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
+            listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1));
+            listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2));
+            listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_3));
+            listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_4));
+            listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_5));
+            listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_6));
+            listRowAdapter.add(new PhotoItem("Android TV", R.drawable.gallery_photo_7));
+            listRowAdapter.add(new PhotoItem("Leanback", R.drawable.gallery_photo_8));
+            HeaderItem header = new HeaderItem(i, "Row " + i);
+            mRowsAdapter.add(new ListRow(header, listRowAdapter));
+        }
+
+        setAdapter(mRowsAdapter);
+    }
+
+    private final class ItemViewClickedListener implements OnItemViewClickedListener {
+        @Override
+        public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
+                RowPresenter.ViewHolder rowViewHolder, Row row) {
+            Intent intent = new Intent(getActivity(), DetailsActivity.class);
+            intent.putExtra(DetailsActivity.EXTRA_ITEM, (PhotoItem) item);
+
+            Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
+                    getActivity(),
+                    ((ImageCardView)itemViewHolder.view).getMainImageView(),
+                    DetailsActivity.SHARED_ELEMENT_NAME).toBundle();
+            getActivity().startActivity(intent, bundle);
+        }
+    }
+}