Add sample activity for ViewPager in Support4Demos

Bug: 23467280
Change-Id: I20453e15e5fb7b235da910d0c290641bd1c3d4c8
diff --git a/samples/Support4Demos/AndroidManifest.xml b/samples/Support4Demos/AndroidManifest.xml
index f1899ad..da5dcaf 100644
--- a/samples/Support4Demos/AndroidManifest.xml
+++ b/samples/Support4Demos/AndroidManifest.xml
@@ -297,6 +297,14 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".view.ViewPagerActivity"
+                  android:label="@string/view_pager_layout_support">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".widget.SwipeRefreshLayoutActivity"
                   android:label="@string/swipe"
                   android:theme="@style/ThemeHoloLight">
@@ -351,14 +359,6 @@
             </intent-filter>
         </activity>
 
-        <activity android:name=".view.ViewPagerActivity"
-                  android:label="@string/view_pager_support">
-            <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
-                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
-            </intent-filter>
-        </activity>
-
         <activity android:name=".graphics.RoundedBitmapDrawableActivity"
             android:label="Graphics/RoundedBitmapDrawable">
             <intent-filter>
diff --git a/samples/Support4Demos/res/layout/view_pager_tab.xml b/samples/Support4Demos/res/layout/view_pager_layout.xml
similarity index 63%
rename from samples/Support4Demos/res/layout/view_pager_tab.xml
rename to samples/Support4Demos/res/layout/view_pager_layout.xml
index 1d0bf31..de248df 100644
--- a/samples/Support4Demos/res/layout/view_pager_tab.xml
+++ b/samples/Support4Demos/res/layout/view_pager_layout.xml
@@ -14,8 +14,17 @@
      limitations under the License.
 -->
 
-<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+<android.support.v4.view.ViewPager
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/pager"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:textSize="32sp"
-    android:gravity="center"/>
+    android:layout_height="match_parent">
+
+    <android.support.v4.view.PagerTitleStrip
+        android:id="@+id/titles"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_gravity="top" />
+
+</android.support.v4.view.ViewPager>
+
diff --git a/samples/Support4Demos/res/layout/view_pager_sample.xml b/samples/Support4Demos/res/layout/view_pager_sample.xml
deleted file mode 100644
index 1c6d238..0000000
--- a/samples/Support4Demos/res/layout/view_pager_sample.xml
+++ /dev/null
@@ -1,57 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 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.
--->
-
-<LinearLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-    <android.support.v4.view.ViewPager
-        android:id="@+id/view_pager"
-        android:layout_width="match_parent"
-        android:layout_height="0dip"
-        android:layout_weight="1">
-        <android.support.v4.view.PagerTabStrip
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_gravity="top"/>
-    </android.support.v4.view.ViewPager>
-
-    <LinearLayout
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:orientation="horizontal"
-        android:gravity="end">
-        <CheckBox
-            android:id="@+id/view_pager_smooth_scroll"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:checked="true"
-            android:text="@string/view_pager_smooth_scroll"/>
-        <Button
-            android:id="@+id/view_pager_switch_tabs_button"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_marginLeft="12dip"
-            android:text="@string/view_pager_switch_tabs"/>
-        <Button
-            android:id="@+id/view_pager_double_switch_tabs_button"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_marginLeft="12dip"
-            android:text="@string/view_pager_double_switch_tabs"/>
-    </LinearLayout>
-</LinearLayout>
diff --git a/samples/Support4Demos/res/values/strings.xml b/samples/Support4Demos/res/values/strings.xml
index 1945a18..1d9f129 100644
--- a/samples/Support4Demos/res/values/strings.xml
+++ b/samples/Support4Demos/res/values/strings.xml
@@ -156,6 +156,8 @@
 
     <string name="sample_transport_controller_activity">Media/TransportController</string>
 
+    <string name="view_pager_layout_support">View/View pager</string>
+
     <string name="sliding_pane_layout_support">Widget/Sliding pane layout</string>
 
     <string name="sliding_pane_layout_summary">This activity illustrates the use of sliding panes. The content pane may be slid to one side on narrow devices to reveal the left pane used to select content. Sliding panes can be used to fit a UI intended for wider screens in a smaller space. Tapping the Action Bar\'s Up button at the left side of the bar will navigate up in the hierarchy, represented by the left pane. If you rotate the device to landscape mode, on most devices you will see that both panes fit together side by side with no sliding necessary.</string>
@@ -181,10 +183,4 @@
     <string name="drawable_compat_no_tint">Not tint</string>
     <string name="drawable_compat_color_tint">Color tint</string>
     <string name="drawable_compat_color_list_tint">Color state list</string>
-
-    <!-- ViewPager -->
-    <string name="view_pager_support">View/View pager</string>
-    <string name="view_pager_smooth_scroll">Smooth scroll</string>
-    <string name="view_pager_switch_tabs">Switch tabs</string>
-    <string name="view_pager_double_switch_tabs">Double-switch tabs</string>
 </resources>
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/view/ViewPagerActivity.java b/samples/Support4Demos/src/com/example/android/supportv4/view/ViewPagerActivity.java
index e50cc61..5150363 100644
--- a/samples/Support4Demos/src/com/example/android/supportv4/view/ViewPagerActivity.java
+++ b/samples/Support4Demos/src/com/example/android/supportv4/view/ViewPagerActivity.java
@@ -14,97 +14,97 @@
  * limitations under the License.
  */
 
-
 package com.example.android.supportv4.view;
 
-import android.os.Bundle;
-import android.support.v4.app.Fragment;
-import android.support.v4.app.FragmentActivity;
-import android.support.v4.app.FragmentStatePagerAdapter;
-import android.support.v4.view.ViewPager;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.Button;
-import android.widget.CheckBox;
-import android.widget.TextView;
-import android.widget.Toast;
 import com.example.android.supportv4.R;
 
-import java.lang.Override;
-import java.lang.Runnable;
+import android.app.Activity;
+import android.graphics.Color;
+import android.os.Bundle;
+import android.support.v4.view.PagerAdapter;
+import android.support.v4.view.PagerTitleStrip;
+import android.support.v4.view.ViewPager;
+import android.util.Pair;
+import android.view.View;
+import android.view.ViewGroup;
 
-public class ViewPagerActivity extends FragmentActivity {
-    private static int[] PAGE_COLORS = { 0xFF700000, 0xFF500020, 0xFF300030, 0xFF200050,
-            0xFF000070};
+import java.util.ArrayList;
+
+public class ViewPagerActivity extends Activity {
+    private ViewPager mPager;
+    private PagerTitleStrip mTitles;
+    private ColorPagerAdapter mAdapter;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        setContentView(R.layout.view_pager_sample);
+        setContentView(R.layout.view_pager_layout);
 
-        final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
-        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
-            @Override
-            public int getCount() {
-                return PAGE_COLORS.length;
-            }
+        mAdapter = new ColorPagerAdapter();
+        mAdapter.add("Red", Color.RED);
+        mAdapter.add("Green", Color.GREEN);
+        mAdapter.add("Blue", Color.BLUE);
 
-            @Override
-            public CharSequence getPageTitle(int position) {
-                return "Page " + position;
-            }
+        mPager = (ViewPager) findViewById(R.id.pager);
+        mPager.setAdapter(mAdapter);
 
-            @Override
-            public Fragment getItem(int position) {
-                Fragment fragment = new DemoObjectFragment();
-                Bundle args = new Bundle();
-                args.putInt(DemoObjectFragment.ARG_INDEX, position);
-                fragment.setArguments(args);
-                return fragment;
-            }
-        });
-
-        final CheckBox smoothScroll = (CheckBox) findViewById(R.id.view_pager_smooth_scroll);
-
-        Button switchTabsButton = (Button) findViewById(R.id.view_pager_switch_tabs_button);
-        switchTabsButton.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                viewPager.setCurrentItem(2, smoothScroll.isChecked());
-                Toast.makeText(view.getContext(), "Current item = " + viewPager.getCurrentItem(),
-                        Toast.LENGTH_SHORT).show();
-            }
-        });
-
-        Button doubleSwitchTabsButton =
-                (Button) findViewById(R.id.view_pager_double_switch_tabs_button);
-        doubleSwitchTabsButton.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                viewPager.setCurrentItem(0, smoothScroll.isChecked());
-                viewPager.setCurrentItem(2, smoothScroll.isChecked());
-                Toast.makeText(view.getContext(), "Current item = " + viewPager.getCurrentItem(),
-                        Toast.LENGTH_SHORT).show();
-            }
-        });
+        mTitles = (PagerTitleStrip) findViewById(R.id.titles);
     }
 
-    public static class DemoObjectFragment extends Fragment {
-        public static final String ARG_INDEX = "index";
+    private static class ColorPagerAdapter extends PagerAdapter {
+        private ArrayList<Pair<String, Integer>> mEntries = new ArrayList<>();
+
+        public void add(String title, int color) {
+            mEntries.add(new Pair(title, color));
+        }
 
         @Override
-        public View onCreateView(LayoutInflater inflater,
-                ViewGroup container, Bundle savedInstanceState) {
-            // The last two arguments ensure LayoutParams are inflated
-            // properly.
-            View rootView = inflater.inflate(R.layout.view_pager_tab, container, false);
-            Bundle args = getArguments();
-            int position = args.getInt(ARG_INDEX);
-            rootView.setBackgroundColor(PAGE_COLORS[position]);
-            ((TextView) rootView).setText(Integer.toString(position));
-            return rootView;
+        public int getCount() {
+            return mEntries.size();
+        }
+
+        @Override
+        public Object instantiateItem(ViewGroup container, int position) {
+            final View view = new View(container.getContext());
+            view.setBackgroundColor(mEntries.get(position).second);
+
+            // Unlike ListView adapters, the ViewPager adapter is responsible
+            // for adding the view to the container.
+            container.addView(view);
+
+            return new ViewHolder(view, position);
+        }
+
+        @Override
+        public void destroyItem(ViewGroup container, int position, Object object) {
+            // The adapter is also responsible for removing the view.
+            container.removeView(((ViewHolder) object).view);
+        }
+
+        @Override
+        public int getItemPosition(Object object) {
+            return ((ViewHolder) object).position;
+        }
+
+        @Override
+        public boolean isViewFromObject(View view, Object object) {
+            return ((ViewHolder) object).view == view;
+        }
+
+        @Override
+        public CharSequence getPageTitle(int position) {
+            return mEntries.get(position).first;
+        }
+
+        private static class ViewHolder {
+            final View view;
+            final int position;
+
+            public ViewHolder(View view, int position) {
+                this.view = view;
+                this.position = position;
+            }
         }
     }
 }