Update pager demos to match new v4 ViewPager API.

Change-Id: I3b0f3382d56b477868b5a5fbf5af22fa83c3e06a
This commit is contained in:
Dianne Hackborn
2011-06-06 14:03:46 -07:00
parent 47f569e460
commit 7ffb7d4b2f
12 changed files with 200 additions and 28 deletions

View File

@@ -19,7 +19,7 @@ document.write(""+
"together. Current samples are only for the fragment and loader in the "+ "together. Current samples are only for the fragment and loader in the "+
"application part of the support library:</p>"+ "application part of the support library:</p>"+
"<ul><li><a href='src/com/example/android/supportv4/app/index.html'>App</a></li></ul>"); "<ul><li><a href='src/com/example/android/supportv13/app/index.html'>App</a></li></ul>");
} }

View File

@@ -21,12 +21,12 @@
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v13.view.ViewPager <android.support.v4.view.ViewPager
android:id="@+id/pager" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0px" android:layout_height="0px"
android:layout_weight="1"> android:layout_weight="1">
</android.support.v13.view.ViewPager> </android.support.v4.view.ViewPager>
<LinearLayout android:orientation="horizontal" <LinearLayout android:orientation="horizontal"
android:gravity="center" android:measureWithLargestChild="true" android:gravity="center" android:measureWithLargestChild="true"

View File

@@ -20,7 +20,7 @@ import com.example.android.supportv13.Cheeses;
import com.example.android.supportv13.R; import com.example.android.supportv13.R;
import android.support.v13.app.FragmentPagerAdapter; import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.view.ViewPager; import android.support.v4.view.ViewPager;
import android.os.Bundle; import android.os.Bundle;
import android.app.Activity; import android.app.Activity;

View File

@@ -20,7 +20,7 @@ import com.example.android.supportv13.Cheeses;
import com.example.android.supportv13.R; import com.example.android.supportv13.R;
import android.support.v13.app.FragmentStatePagerAdapter; import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v13.view.ViewPager; import android.support.v4.view.ViewPager;
import android.os.Bundle; import android.os.Bundle;
import android.app.Activity; import android.app.Activity;

View File

@@ -9,7 +9,12 @@ package features of the static support library fir API 13 or later.
<h3 id="Fragment">Fragment</h3> <h3 id="Fragment">Fragment</h3>
<dl> <dl>
<dt><a href="FragmentPagerSupport.html">Fragment Pager Support</a></dt> <dt><a href="FragmentPagerSupport.html">Fragment Pager Support</a></dt>
<dd>A support class for using the framework Fragment APIs to build <dd>Demonstrates the use of the v4 support class ViewPager with a
a user interface where the user can fling left or right to switch FragmentPagerAdapter to build a user interface where the user can fling
between fragments.</dd> left or right to switch between fragments.</dd>
<dt><a href="FragmentStatePagerSupport.html">Fragment State Pager Support</a></dt>
<dd>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.</dd>
</dl> </dl>

View File

@@ -24,7 +24,7 @@
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-sdk android:minSdkVersion="4" /> <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="13" />
<!-- This app has not been optimized for large screens. --> <!-- This app has not been optimized for large screens. -->
<supports-screens android:requiresSmallestWidthDp="320" <supports-screens android:requiresSmallestWidthDp="320"
@@ -155,6 +155,14 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentStatePagerSupport"
android:label="@string/fragment_state_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=".app.LoaderCursorSupport" <activity android:name=".app.LoaderCursorSupport"
android:label="@string/loader_cursor_support"> android:label="@string/loader_cursor_support">
<intent-filter> <intent-filter>

View File

@@ -21,12 +21,12 @@
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v4.app.FragmentPager <android.support.v4.view.ViewPager
android:id="@+id/pager" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0px" android:layout_height="0px"
android:layout_weight="1"> android:layout_weight="1">
</android.support.v4.app.FragmentPager> </android.support.v4.view.ViewPager>
<LinearLayout android:orientation="horizontal" <LinearLayout android:orientation="horizontal"
android:gravity="center" android:measureWithLargestChild="true" android:gravity="center" android:measureWithLargestChild="true"

View File

@@ -81,6 +81,8 @@
<string name="first">First</string> <string name="first">First</string>
<string name="last">Last</string> <string name="last">Last</string>
<string name="fragment_state_pager_support">Fragment/State Pager</string>
<string name="loader_cursor_support">Loader/Cursor</string> <string name="loader_cursor_support">Loader/Cursor</string>
<string name="loader_throttle_support">Loader/Throttle</string> <string name="loader_throttle_support">Loader/Throttle</string>

View File

@@ -22,8 +22,10 @@ import com.example.android.supportv4.R;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; 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.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@@ -34,19 +36,22 @@ import android.widget.Button;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
public class FragmentPagerSupport extends FragmentActivity public class FragmentPagerSupport extends FragmentActivity {
implements FragmentPager.Adapter {
static final int NUM_ITEMS = 10; static final int NUM_ITEMS = 10;
FragmentPager mPager; MyAdapter mAdapter;
ViewPager mPager;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager); setContentView(R.layout.fragment_pager);
mPager = (FragmentPager)findViewById(R.id.pager); mAdapter = new MyAdapter(getSupportFragmentManager());
mPager.setAdapter(this);
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
// Watch for button clicks. // Watch for button clicks.
Button button = (Button)findViewById(R.id.goto_first); Button button = (Button)findViewById(R.id.goto_first);
@@ -63,14 +68,20 @@ public class FragmentPagerSupport extends FragmentActivity
}); });
} }
@Override public static class MyAdapter extends FragmentPagerAdapter {
public int getCount() { public MyAdapter(FragmentManager fm) {
return NUM_ITEMS; super(fm);
} }
@Override @Override
public Fragment getItem(int position) { public int getCount() {
return ArrayListFragment.newInstance(position); return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
} }
public static class ArrayListFragment extends ListFragment { public static class ArrayListFragment extends ListFragment {

View File

@@ -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<String>(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);
}
}
}

View File

@@ -63,7 +63,7 @@ public class LoaderThrottleSupport extends FragmentActivity {
/** /**
* The authority we use to get to our sample provider. * 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. * Definition of the contract for the main table of our provider.

View File

@@ -45,9 +45,15 @@ and loaders.</p>
<dd>Demonstrates populating custom menu items from a Fragment.</dd> <dd>Demonstrates populating custom menu items from a Fragment.</dd>
<dt><a href="FragmentPagerSupport.html">Fragment Pager Support</a></dt> <dt><a href="FragmentPagerSupport.html">Fragment Pager Support</a></dt>
<dd>A support class for using the Fragment APIs to build <dd>Demonstrates the use of the support class ViewPager with a
a user interface where the user can fling left or right to switch FragmentPagerAdapter to build a user interface where the user can fling
between fragments.</dd> left or right to switch between fragments.</dd>
<dt><a href="FragmentStatePagerSupport.html">Fragment State Pager Support</a></dt>
<dd>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.</dd>
<dt><a href="FragmentReceiveResultSupport.html">Fragment Receive Result</a></dt> <dt><a href="FragmentReceiveResultSupport.html">Fragment Receive Result</a></dt>
<dd>Demonstrates starting a new Activity from a Fragment, and receiving <dd>Demonstrates starting a new Activity from a Fragment, and receiving