From 7ffb7d4b2face7d2ce5cee82c92ec36fd0ba2932 Mon Sep 17 00:00:00 2001
From: Dianne Hackborn
Date: Mon, 6 Jun 2011 14:03:46 -0700
Subject: [PATCH] Update pager demos to match new v4 ViewPager API.
Change-Id: I3b0f3382d56b477868b5a5fbf5af22fa83c3e06a
---
samples/Support13Demos/_index.html | 2 +-
.../res/layout/fragment_pager.xml | 4 +-
.../supportv13/app/FragmentPagerSupport.java | 2 +-
.../app/FragmentStatePagerSupport.java | 2 +-
.../android/supportv13/app/_index.html | 11 +-
samples/Support4Demos/AndroidManifest.xml | 10 +-
.../res/layout/fragment_pager.xml | 4 +-
samples/Support4Demos/res/values/strings.xml | 2 +
.../supportv4/app/FragmentPagerSupport.java | 37 +++--
.../app/FragmentStatePagerSupport.java | 140 ++++++++++++++++++
.../supportv4/app/LoaderThrottleSupport.java | 2 +-
.../example/android/supportv4/app/_index.html | 12 +-
12 files changed, 200 insertions(+), 28 deletions(-)
create mode 100644 samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.java
diff --git a/samples/Support13Demos/_index.html b/samples/Support13Demos/_index.html
index d6168aba7..5861f72e9 100644
--- a/samples/Support13Demos/_index.html
+++ b/samples/Support13Demos/_index.html
@@ -19,7 +19,7 @@ document.write(""+
"together. Current samples are only for the fragment and loader in the "+
"application part of the support library:
"+
-"");
+"");
}
diff --git a/samples/Support13Demos/res/layout/fragment_pager.xml b/samples/Support13Demos/res/layout/fragment_pager.xml
index cc07d84df..46c8cbe0e 100644
--- a/samples/Support13Demos/res/layout/fragment_pager.xml
+++ b/samples/Support13Demos/res/layout/fragment_pager.xml
@@ -21,12 +21,12 @@
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
-
-
+
Fragment
- Fragment Pager Support
- - A support class for using the framework Fragment APIs to build
- a user interface where the user can fling left or right to switch
- between fragments.
+ - Demonstrates the use of the v4 support class ViewPager with a
+ FragmentPagerAdapter to build a user interface where the user can fling
+ left or right to switch between fragments.
+ - Fragment State Pager Support
+ - Demonstrates the use of the v4 support class ViewPager with a
+ FragmentStatePagerAdapter to build a user interface where the user can fling
+ left or right to switch between fragments. This versions of the adapter
+ doesn't keep around the fragment instances that ViewPager has destroyed.
diff --git a/samples/Support4Demos/AndroidManifest.xml b/samples/Support4Demos/AndroidManifest.xml
index 772eb5453..db4dbd12c 100644
--- a/samples/Support4Demos/AndroidManifest.xml
+++ b/samples/Support4Demos/AndroidManifest.xml
@@ -24,7 +24,7 @@
-
+
+
+
+
+
+
+
+
diff --git a/samples/Support4Demos/res/layout/fragment_pager.xml b/samples/Support4Demos/res/layout/fragment_pager.xml
index a082e2e54..46c8cbe0e 100644
--- a/samples/Support4Demos/res/layout/fragment_pager.xml
+++ b/samples/Support4Demos/res/layout/fragment_pager.xml
@@ -21,12 +21,12 @@
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
-
-
+
First
Last
+ Fragment/State Pager
+
Loader/Cursor
Loader/Throttle
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java
index 3329597b6..c10890bd0 100644
--- a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java
@@ -22,8 +22,10 @@ import com.example.android.supportv4.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
-import android.support.v4.app.FragmentPager;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
+import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@@ -34,19 +36,22 @@ import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
-public class FragmentPagerSupport extends FragmentActivity
- implements FragmentPager.Adapter {
+public class FragmentPagerSupport extends FragmentActivity {
static final int NUM_ITEMS = 10;
- FragmentPager mPager;
+ MyAdapter mAdapter;
+
+ ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
- mPager = (FragmentPager)findViewById(R.id.pager);
- mPager.setAdapter(this);
+ mAdapter = new MyAdapter(getSupportFragmentManager());
+
+ mPager = (ViewPager)findViewById(R.id.pager);
+ mPager.setAdapter(mAdapter);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.goto_first);
@@ -63,14 +68,20 @@ public class FragmentPagerSupport extends FragmentActivity
});
}
- @Override
- public int getCount() {
- return NUM_ITEMS;
- }
+ public static class MyAdapter extends FragmentPagerAdapter {
+ public MyAdapter(FragmentManager fm) {
+ super(fm);
+ }
- @Override
- public Fragment getItem(int position) {
- return ArrayListFragment.newInstance(position);
+ @Override
+ public int getCount() {
+ return NUM_ITEMS;
+ }
+
+ @Override
+ public Fragment getItem(int position) {
+ return ArrayListFragment.newInstance(position);
+ }
}
public static class ArrayListFragment extends ListFragment {
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.java
new file mode 100644
index 000000000..8a5289635
--- /dev/null
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.java
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2011 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.supportv4.app;
+
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentStatePagerAdapter;
+import android.support.v4.app.ListFragment;
+import android.support.v4.view.ViewPager;
+
+import android.os.Bundle;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnClickListener;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.ListView;
+import android.widget.TextView;
+
+import com.example.android.supportv4.Cheeses;
+import com.example.android.supportv4.R;
+
+public class FragmentStatePagerSupport extends FragmentActivity {
+ static final int NUM_ITEMS = 10;
+
+ MyAdapter mAdapter;
+
+ ViewPager mPager;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.fragment_pager);
+
+ mAdapter = new MyAdapter(getSupportFragmentManager());
+
+ mPager = (ViewPager)findViewById(R.id.pager);
+ mPager.setAdapter(mAdapter);
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.goto_first);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ mPager.setCurrentItem(0);
+ }
+ });
+ button = (Button)findViewById(R.id.goto_last);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ mPager.setCurrentItem(NUM_ITEMS-1);
+ }
+ });
+ }
+
+ public static class MyAdapter extends FragmentStatePagerAdapter {
+ public MyAdapter(FragmentManager fm) {
+ super(fm);
+ }
+
+ @Override
+ public int getCount() {
+ return NUM_ITEMS;
+ }
+
+ @Override
+ public Fragment getItem(int position) {
+ return ArrayListFragment.newInstance(position);
+ }
+ }
+
+ public static class ArrayListFragment extends ListFragment {
+ int mNum;
+
+ /**
+ * Create a new instance of CountingFragment, providing "num"
+ * as an argument.
+ */
+ static ArrayListFragment newInstance(int num) {
+ ArrayListFragment f = new ArrayListFragment();
+
+ // Supply num input as an argument.
+ Bundle args = new Bundle();
+ args.putInt("num", num);
+ f.setArguments(args);
+
+ return f;
+ }
+
+ /**
+ * When creating, retrieve this instance's number from its arguments.
+ */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ mNum = getArguments() != null ? getArguments().getInt("num") : 1;
+ }
+
+ /**
+ * The Fragment's UI is just a simple text view showing its
+ * instance number.
+ */
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
+ View tv = v.findViewById(R.id.text);
+ ((TextView)tv).setText("Fragment #" + mNum);
+ return v;
+ }
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+ setListAdapter(new ArrayAdapter(getActivity(),
+ android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
+ }
+
+ @Override
+ public void onListItemClick(ListView l, View v, int position, long id) {
+ Log.i("FragmentList", "Item clicked: " + id);
+ }
+ }
+}
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderThrottleSupport.java b/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderThrottleSupport.java
index 669300805..d16797bc5 100644
--- a/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderThrottleSupport.java
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/LoaderThrottleSupport.java
@@ -63,7 +63,7 @@ public class LoaderThrottleSupport extends FragmentActivity {
/**
* The authority we use to get to our sample provider.
*/
- public static final String AUTHORITY = "com.example.android.apis.support.app.LoaderThrottle";
+ public static final String AUTHORITY = "com.example.android.apis.supportv4.app.LoaderThrottle";
/**
* Definition of the contract for the main table of our provider.
diff --git a/samples/Support4Demos/src/com/example/android/supportv4/app/_index.html b/samples/Support4Demos/src/com/example/android/supportv4/app/_index.html
index 592ccc16b..286d4a0ff 100644
--- a/samples/Support4Demos/src/com/example/android/supportv4/app/_index.html
+++ b/samples/Support4Demos/src/com/example/android/supportv4/app/_index.html
@@ -45,9 +45,15 @@ and loaders.
Demonstrates populating custom menu items from a Fragment.
Fragment Pager Support
- A support class for using the Fragment APIs to build
- a user interface where the user can fling left or right to switch
- between fragments.
+ Demonstrates the use of the support class ViewPager with a
+ FragmentPagerAdapter to build a user interface where the user can fling
+ left or right to switch between fragments.
+
+ Fragment State Pager Support
+ Demonstrates the use of the support class ViewPager with a
+ FragmentStatePagerAdapter to build a user interface where the user can fling
+ left or right to switch between fragments. This versions of the adapter
+ doesn't keep around the fragment instances that ViewPager has destroyed.
Fragment Receive Result
Demonstrates starting a new Activity from a Fragment, and receiving