[ThemePicker 7/N] Initial setup of ThemeFragment UI

Add a view for the theme preview card for font, and hook up
the option tiles as well.

Bug: 120559294

Change-Id: I7efa779e9db730f8aaff151eac65bee42ed5db6b
diff --git a/res/drawable/ic_font.xml b/res/drawable/ic_font.xml
new file mode 100644
index 0000000..559d267
--- /dev/null
+++ b/res/drawable/ic_font.xml
@@ -0,0 +1,29 @@
+<!--
+     Copyright (C) 2019 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.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="36dp"
+    android:height="36dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+  <!-- This path represents a square with rounded corners -->
+  <path
+      android:fillColor="#FF000000"
+      android:pathData="M20,2H4C2.9,2 2,2.9 2,4v16c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2V4C22,2.9 21.1,2 20,2zM20,20H4V4h16V20z"/>
+  <!-- This path represents a letter "A" inside the square -->
+  <path
+      android:fillColor="#FF000000"
+      android:pathData="M10.69,6h2.6l4.51,12h-2.5l-1.01,-2.87H9.7L8.7,18H6.2L10.69,6zM13.56,13.06l-1.06,-3.02L12.07,8.6h-0.13l-0.44,1.44l-1.07,3.02H13.56z"/>
+</vector>
diff --git a/res/drawable/ic_nav_clock.xml b/res/drawable/ic_nav_clock.xml
new file mode 100644
index 0000000..4fa1d05
--- /dev/null
+++ b/res/drawable/ic_nav_clock.xml
@@ -0,0 +1,30 @@
+<!--
+     Copyright (C) 2019 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.
+-->
+<!-- Represents a simple circular clock -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+  <!-- This path corresponds to the outside circle -->
+  <path
+      android:fillColor="#FF000000"
+      android:pathData="M11.99,2C6.47,2 2,6.48 2,12c0,5.52 4.47,10 9.99,10C17.52,22 22,17.52 22,12C22,6.48 17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8c0,-4.42 3.58,-8 8,-8s8,3.58 8,8C20,16.42 16.42,20 12,20z"/>
+  <!-- This draws two lines at an angle representing the clock hands -->
+  <path
+      android:fillColor="#FF000000"
+      android:pathData="M16.49,16.36l-5.49,-3.29l0,-6.65l2,0l0,5.51l4.51,2.71z"/>
+</vector>
diff --git a/res/drawable/ic_nav_grid.xml b/res/drawable/ic_nav_grid.xml
new file mode 100644
index 0000000..fd946ed
--- /dev/null
+++ b/res/drawable/ic_nav_grid.xml
@@ -0,0 +1,25 @@
+<!--
+     Copyright (C) 2019 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.
+-->
+<!-- Represents a grid of lines -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+  <path
+      android:fillColor="#FF000000"
+      android:pathData="M22,7V5h-3V2h-2v3h-4V2h-2v3H7V2H5v3H2v2h3v4H2v2h3v4H2v2h3v3h2v-3h4v3h2v-3h4v3h2v-3h3v-2h-3v-4h3v-2h-3V7H22zM7,7h4v4H7V7zM7,17v-4h4v4H7zM17,17h-4v-4h4V17zM17,11h-4V7h4V11z"/>
+</vector>
diff --git a/res/drawable/ic_nav_theme.xml b/res/drawable/ic_nav_theme.xml
index 52e3961..4b277a4 100644
--- a/res/drawable/ic_nav_theme.xml
+++ b/res/drawable/ic_nav_theme.xml
@@ -1,13 +1,25 @@
-<!-- TODO (santie): replace with correct asset once available -->
+<!--
+     Copyright (C) 2019 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.
+-->
+<!-- This draws a paintbrush like shape -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-    android:width="24dp"
-    android:height="24dp"
-    android:viewportWidth="24"
-    android:viewportHeight="24">
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24"
+        android:viewportHeight="24">
     <path
         android:fillColor="#FF000000"
-        android:pathData="M12,2C6.48,2 2,6.48 2,12c0,5.52 4.48,10 10,10s10,-4.48 10,-10C22,6.48 17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8c0,-4.41 3.59,-8 8,-8s8,3.59 8,8C20,16.41 16.41,20 12,20z"/>
-    <path
-        android:fillColor="#FF000000"
-        android:pathData="M6.5,17.5l7.51,-3.49L17.5,6.5L9.99,9.99L6.5,17.5zM12,10.9c0.61,0 1.1,0.49 1.1,1.1s-0.49,1.1 -1.1,1.1s-1.1,-0.49 -1.1,-1.1S11.39,10.9 12,10.9z"/>
-</vector>
+        android:pathData="M4,2v9c0,1.65 1.35,3 3,3h2v6c0,1.1 0.9,2 2,2h2c1.1,0 2,-0.9 2,-2v-6h2c1.65,0 3,-1.35 3,-3V2C20,2 4,2 4,2zM11,20v-6h2v6H11zM18,11c0,0.55 -0.45,1 -1,1h-2H9H7c-0.55,0 -1,-0.45 -1,-1v-0.93h12V11zM18,8.07H6V4h2.81v2.15h2V4h2.38v2.15h2V4H18V8.07z"/>
+</vector>
\ No newline at end of file
diff --git a/res/drawable/ic_nav_wallpaper.xml b/res/drawable/ic_nav_wallpaper.xml
new file mode 100644
index 0000000..b18a36f
--- /dev/null
+++ b/res/drawable/ic_nav_wallpaper.xml
@@ -0,0 +1,25 @@
+<!--
+     Copyright (C) 2019 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.
+-->
+<!-- Represents the wallpaper icon (a "landscape" shape in a box) -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+  <path
+      android:fillColor="#FF000000"
+      android:pathData="M9,12.71l2.14,2.58l3,-3.87L18,16.57H6L9,12.71zM5,5h6V3H5C3.9,3 3,3.9 3,5v6h2V5zM19,19h-6v2h6c1.1,0 2,-0.9 2,-2v-6h-2V19zM5,19v-6H3v6c0,1.1 0.9,2 2,2h6v-2H5zM19,5v6h2V5c0,-1.1 -0.9,-2 -2,-2h-6v2H19zM16,9c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1c-0.55,0 -1,0.45 -1,1S15.45,9 16,9z"/>
+</vector>
diff --git a/res/layout/fragment_theme_picker.xml b/res/layout/fragment_theme_picker.xml
index 95702f1..0fd4044 100644
--- a/res/layout/fragment_theme_picker.xml
+++ b/res/layout/fragment_theme_picker.xml
@@ -22,7 +22,12 @@
     android:background="@color/category_picker_background_color">
     <include layout="@layout/section_header"/>
 
-    <!-- TODO(santie): Preview pager goes here... -->
+    <com.android.customization.widget.PreviewPager
+        android:id="@+id/theme_preview_pager"
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1"
+        android:background="@color/secondary_color"/>
 
     <LinearLayout
         android:id="@+id/options_section"
diff --git a/res/layout/preview_card_font_content.xml b/res/layout/preview_card_font_content.xml
new file mode 100644
index 0000000..bc66f20
--- /dev/null
+++ b/res/layout/preview_card_font_content.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2019 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"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="wrap_content"
+    android:layout_height="match_parent"
+    android:layout_gravity="center"
+    android:gravity="center_horizontal"
+    android:orientation="vertical"
+    tools:showIn="@layout/theme_preview_card">
+    <TextView
+        style="@style/FontCardTitleStyle"
+        android:id="@+id/font_card_title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginBottom="16dp"
+        android:gravity="center_horizontal"
+        android:text="@string/font_card_title"/>
+    <TextView
+        style="@style/FontCardBodyTextStyle"
+        android:id="@+id/font_card_body"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:gravity="center_horizontal"
+        android:text="@string/font_card_body"/>
+</LinearLayout>
\ No newline at end of file
diff --git a/res/layout/theme_option.xml b/res/layout/theme_option.xml
index fc08f6b..9e2ef82 100644
--- a/res/layout/theme_option.xml
+++ b/res/layout/theme_option.xml
@@ -63,4 +63,4 @@
             android:layout_alignParentBottom="true"
             android:layout_alignParentRight="true"/>
     </RelativeLayout>
-</LinearLayout>
\ No newline at end of file
+</LinearLayout>
diff --git a/res/layout/theme_preview_card.xml b/res/layout/theme_preview_card.xml
new file mode 100644
index 0000000..0f2e2ea
--- /dev/null
+++ b/res/layout/theme_preview_card.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2019 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.
+-->
+<androidx.cardview.widget.CardView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    style="@style/PreviewCard"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical">
+        <TextView
+            android:id="@+id/theme_preview_card_header"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center_horizontal"
+            android:layout_marginBottom="8dp"
+            android:drawablePadding="10dp"
+            android:textAppearance="@style/CardTitleTextAppearance"/>
+        <FrameLayout
+            android:id="@+id/theme_preview_card_body_container"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:layout_marginHorizontal="24dp">
+        </FrameLayout>
+    </LinearLayout>
+
+</androidx.cardview.widget.CardView>
\ No newline at end of file
diff --git a/res/menu/bottom_navigation_menu.xml b/res/menu/bottom_navigation_menu.xml
index 4954206..a856222 100644
--- a/res/menu/bottom_navigation_menu.xml
+++ b/res/menu/bottom_navigation_menu.xml
@@ -23,7 +23,7 @@
     <item
         android:id="@+id/nav_clock"
         android:title="@string/clock_title"
-        android:icon="@drawable/ic_nav_theme" />
+        android:icon="@drawable/ic_nav_grid" />
     <item
         android:id="@+id/nav_grid"
         android:title="@string/grid_title"
@@ -31,5 +31,5 @@
     <item
         android:id="@+id/nav_wallpaper"
         android:title="@string/wallpaper_title"
-        android:icon="@drawable/ic_nav_theme" />
+        android:icon="@drawable/ic_nav_wallpaper" />
 </menu>
\ No newline at end of file
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 985ef61..10a40f0 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -44,4 +44,9 @@
     <dimen name="theme_option_sample_left_width">22dp</dimen>
     <dimen name="option_tile_margin_horizontal">4dp</dimen>
     <dimen name="theme_option_label_margin">4dp</dimen>
-</resources>
\ No newline at end of file
+
+    <dimen name="preview_card_corner_radius">8dp</dimen>
+    <dimen name="preview_card_padding">24dp</dimen>
+
+    <dimen name="card_title_text_size">16sp</dimen>
+</resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index abb2988..5991826 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -47,4 +47,13 @@
 
     <!-- Sample text used to show a preview of a selected font [CHAR LIMIT=3] -->
     <string name="theme_font_example">ABC</string>
+
+    <!-- Name of the card that previews a theme's font [CHAR LIMIT=20] -->
+    <string name="preview_name_font">Font</string>
+
+    <!-- Title text for previewing a font [CHAR LIMIT=30] -->
+    <string name="font_card_title">Personalize your Device</string>
+
+    <!-- Body text for previewing a font [CHAR LIMIT=160] -->
+    <string name="font_card_body">My Style on every screen!\nOnce you install a theme, its design extends to every facet of the phone, from the home screen to icons, Quick Settings, etc.</string>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 38e5bdd..01bdca1 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -55,4 +55,27 @@
         <item name="android:textColor">@color/option_title_color</item>
     </style>
 
-</resources>
\ No newline at end of file
+
+    <style name="PreviewCard" parent="CardView.Light">
+        <item name="cardCornerRadius">@dimen/preview_card_corner_radius</item>
+        <item name="android:clipChildren">true</item>
+        <item name="contentPadding">@dimen/preview_card_padding</item>
+        <item name="android:background">@color/primary_color</item>
+    </style>
+
+    <style name="CardTitleTextAppearance" parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title">
+        <item name="android:textStyle">bold</item>
+        <item name="android:textSize">@dimen/card_title_text_size</item>
+    </style>
+
+    <style name="FontCardTitleStyle">
+        <item name="android:textAlignment">center</item>
+        <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title</item>
+    </style>
+
+    <style name="FontCardBodyTextStyle">
+        <item name="android:textAlignment">center</item>
+        <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault</item>
+    </style>
+
+</resources>
diff --git a/src/com/android/customization/picker/theme/ThemeFragment.java b/src/com/android/customization/picker/theme/ThemeFragment.java
index 5f0610c..6cb03ff 100644
--- a/src/com/android/customization/picker/theme/ThemeFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFragment.java
@@ -15,17 +15,37 @@
  */
 package com.android.customization.picker.theme;
 
+import static androidx.core.view.ViewCompat.LAYOUT_DIRECTION_RTL;
+
+import android.content.res.ColorStateList;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.TextView;
 
+import androidx.annotation.ColorInt;
+import androidx.annotation.DrawableRes;
+import androidx.annotation.LayoutRes;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.annotation.StringRes;
+import androidx.cardview.widget.CardView;
+import androidx.core.view.ViewCompat;
+import androidx.recyclerview.widget.RecyclerView;
+import androidx.viewpager.widget.PagerAdapter;
 
+import com.android.customization.model.theme.DefaultThemeProvider;
+import com.android.customization.model.theme.ThemeBundle;
+import com.android.customization.model.theme.ThemeManager;
+import com.android.customization.widget.OptionSelectorController;
+import com.android.customization.widget.PreviewPager;
 import com.android.wallpaper.R;
 import com.android.wallpaper.picker.ToolbarFragment;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Fragment that contains the main UI for selecting and applying a ThemeBundle.
  */
@@ -37,6 +57,12 @@
         return fragment;
     }
 
+    private RecyclerView mOptionsContainer;
+    private OptionSelectorController mOptionsController;
+    private ThemeManager mThemeManager;
+    private ThemeBundle mSelectedTheme;
+    private PagerAdapter mAdapter;
+    private PreviewPager mPreviewPager;
 
     @Nullable
     @Override
@@ -45,7 +71,128 @@
         View view = inflater.inflate(
                 R.layout.fragment_theme_picker, container, /* attachToRoot */ false);
         setUpToolbar(view);
+        mPreviewPager = view.findViewById(R.id.theme_preview_pager);
+        mThemeManager = new ThemeManager(new DefaultThemeProvider(getContext()));
+        mOptionsContainer = view.findViewById(R.id.options_container);
+        setUpOptions();
 
         return view;
     }
+
+    private void createAdapter() {
+        mAdapter = new ThemePreviewAdapter(mSelectedTheme);
+        mPreviewPager.setAdapter(mAdapter);
+    }
+
+    private void setUpOptions() {
+        mThemeManager.fetchOptions(options -> {
+            mOptionsController = new OptionSelectorController(mOptionsContainer, options);
+
+            mOptionsController.addListener(selected -> {
+                mSelectedTheme = (ThemeBundle) selected;
+                createAdapter();
+            });
+            mOptionsController.initOptions();
+            for (ThemeBundle theme : options) {
+                if (theme.isCurrentlySet()) {
+                    mSelectedTheme = theme;
+                }
+            }
+            // For development only, as there should always be a theme set.
+            if (mSelectedTheme == null) {
+                mSelectedTheme = options.get(0);
+            }
+        });
+        createAdapter();
+    }
+
+    /**
+     * Adapter class for mPreviewPager.
+     * This is a ViewPager as it allows for a nice pagination effect (ie, pages snap on swipe,
+     * we don't want to just scroll)
+     */
+    private class ThemePreviewAdapter extends PagerAdapter {
+
+        private abstract class PreviewPage {
+            @StringRes final int nameResId;
+            @DrawableRes final int iconSrc;
+            @LayoutRes final int contentLayoutRes;
+            @ColorInt final int accentColor;
+
+            CardView card;
+
+            private PreviewPage(@StringRes int titleResId, @DrawableRes int iconSrc,
+                    @LayoutRes int contentLayoutRes, @ColorInt int accentColor) {
+                this.nameResId = titleResId;
+                this.iconSrc = iconSrc;
+                this.contentLayoutRes = contentLayoutRes;
+                this.accentColor = accentColor;
+            }
+
+            public void setCard(CardView card) {
+                this.card = card;
+            }
+
+            abstract void bindPreviewContent();
+        }
+
+        List<PreviewPage> mPages = new ArrayList<>();
+
+        ThemePreviewAdapter(ThemeBundle theme) {
+            mPages.add(new PreviewPage(R.string.preview_name_font, R.drawable.ic_font,
+                    R.layout.preview_card_font_content, theme.getPreviewInfo().colorAccentLight) {
+                @Override
+                void bindPreviewContent() {
+                    TextView title = card.findViewById(R.id.font_card_title);
+                    title.setTypeface(theme.getPreviewInfo().headlineFontFamily);
+                    TextView body = card.findViewById(R.id.font_card_body);
+                    body.setTypeface(theme.getPreviewInfo().bodyFontFamily);
+                }
+            });
+        }
+
+        @Override
+        public int getCount() {
+            return mPages.size();
+        }
+
+        @Override
+        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
+            return view == ((PreviewPage)object).card;
+        }
+
+        @NonNull
+        @Override
+        public Object instantiateItem(@NonNull ViewGroup container, int position) {
+            if (ViewCompat.getLayoutDirection(container) == LAYOUT_DIRECTION_RTL) {
+                position = mPages.size() - 1 - position;
+            }
+            LayoutInflater inflater = LayoutInflater.from(getContext());
+            CardView card = (CardView) inflater.inflate(R.layout.theme_preview_card,
+                    container, false);
+            PreviewPage page = mPages.get(position);
+
+            TextView header = card.findViewById(R.id.theme_preview_card_header);
+            header.setText(page.nameResId);
+            header.setCompoundDrawablesWithIntrinsicBounds(0, page.iconSrc, 0, 0);
+            header.setCompoundDrawableTintList(ColorStateList.valueOf(page.accentColor));
+
+            ViewGroup body = card.findViewById(R.id.theme_preview_card_body_container);
+            inflater.inflate(page.contentLayoutRes, body, true);
+
+            page.setCard(card);
+            page.bindPreviewContent();
+            if (card.getParent() != null) {
+                container.removeView(card);
+            }
+            container.addView(card);
+            return page;
+        }
+
+        @Override
+        public void destroyItem(@NonNull ViewGroup container, int position,
+                @NonNull Object object) {
+            ((PreviewPage) object).card = null;
+        }
+    }
 }