development
Révision | 7ffb7d4b2face7d2ce5cee82c92ec36fd0ba2932 (tree) |
---|---|
l'heure | 2011-06-07 07:01:45 |
Auteur | Dianne Hackborn <hackbod@goog...> |
Commiter | Dianne Hackborn |
Update pager demos to match new v4 ViewPager API.
Change-Id: I3b0f3382d56b477868b5a5fbf5af22fa83c3e06a
@@ -19,7 +19,7 @@ document.write(""+ | ||
19 | 19 | "together. Current samples are only for the fragment and loader in the "+ |
20 | 20 | "application part of the support library:</p>"+ |
21 | 21 | |
22 | -"<ul><li><a href='src/com/example/android/supportv4/app/index.html'>App</a></li></ul>"); | |
22 | +"<ul><li><a href='src/com/example/android/supportv13/app/index.html'>App</a></li></ul>"); | |
23 | 23 | |
24 | 24 | } |
25 | 25 |
@@ -21,12 +21,12 @@ | ||
21 | 21 | android:gravity="center_horizontal" |
22 | 22 | android:layout_width="match_parent" android:layout_height="match_parent"> |
23 | 23 | |
24 | - <android.support.v13.view.ViewPager | |
24 | + <android.support.v4.view.ViewPager | |
25 | 25 | android:id="@+id/pager" |
26 | 26 | android:layout_width="match_parent" |
27 | 27 | android:layout_height="0px" |
28 | 28 | android:layout_weight="1"> |
29 | - </android.support.v13.view.ViewPager> | |
29 | + </android.support.v4.view.ViewPager> | |
30 | 30 | |
31 | 31 | <LinearLayout android:orientation="horizontal" |
32 | 32 | android:gravity="center" android:measureWithLargestChild="true" |
@@ -20,7 +20,7 @@ import com.example.android.supportv13.Cheeses; | ||
20 | 20 | import com.example.android.supportv13.R; |
21 | 21 | |
22 | 22 | import android.support.v13.app.FragmentPagerAdapter; |
23 | -import android.support.v13.view.ViewPager; | |
23 | +import android.support.v4.view.ViewPager; | |
24 | 24 | |
25 | 25 | import android.os.Bundle; |
26 | 26 | import android.app.Activity; |
@@ -20,7 +20,7 @@ import com.example.android.supportv13.Cheeses; | ||
20 | 20 | import com.example.android.supportv13.R; |
21 | 21 | |
22 | 22 | import android.support.v13.app.FragmentStatePagerAdapter; |
23 | -import android.support.v13.view.ViewPager; | |
23 | +import android.support.v4.view.ViewPager; | |
24 | 24 | |
25 | 25 | import android.os.Bundle; |
26 | 26 | import android.app.Activity; |
@@ -9,7 +9,12 @@ package features of the static support library fir API 13 or later. | ||
9 | 9 | <h3 id="Fragment">Fragment</h3> |
10 | 10 | <dl> |
11 | 11 | <dt><a href="FragmentPagerSupport.html">Fragment Pager Support</a></dt> |
12 | - <dd>A support class for using the framework Fragment APIs to build | |
13 | - a user interface where the user can fling left or right to switch | |
14 | - between fragments.</dd> | |
12 | + <dd>Demonstrates the use of the v4 support class ViewPager with a | |
13 | + FragmentPagerAdapter to build a user interface where the user can fling | |
14 | + left or right to switch between fragments.</dd> | |
15 | + <dt><a href="FragmentStatePagerSupport.html">Fragment State Pager Support</a></dt> | |
16 | + <dd>Demonstrates the use of the v4 support class ViewPager with a | |
17 | + FragmentStatePagerAdapter to build a user interface where the user can fling | |
18 | + left or right to switch between fragments. This versions of the adapter | |
19 | + doesn't keep around the fragment instances that ViewPager has destroyed.</dd> | |
15 | 20 | </dl> |
@@ -24,7 +24,7 @@ | ||
24 | 24 | |
25 | 25 | <uses-permission android:name="android.permission.READ_CONTACTS" /> |
26 | 26 | |
27 | - <uses-sdk android:minSdkVersion="4" /> | |
27 | + <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="13" /> | |
28 | 28 | |
29 | 29 | <!-- This app has not been optimized for large screens. --> |
30 | 30 | <supports-screens android:requiresSmallestWidthDp="320" |
@@ -155,6 +155,14 @@ | ||
155 | 155 | </intent-filter> |
156 | 156 | </activity> |
157 | 157 | |
158 | + <activity android:name=".app.FragmentStatePagerSupport" | |
159 | + android:label="@string/fragment_state_pager_support"> | |
160 | + <intent-filter> | |
161 | + <action android:name="android.intent.action.MAIN" /> | |
162 | + <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" /> | |
163 | + </intent-filter> | |
164 | + </activity> | |
165 | + | |
158 | 166 | <activity android:name=".app.LoaderCursorSupport" |
159 | 167 | android:label="@string/loader_cursor_support"> |
160 | 168 | <intent-filter> |
@@ -21,12 +21,12 @@ | ||
21 | 21 | android:gravity="center_horizontal" |
22 | 22 | android:layout_width="match_parent" android:layout_height="match_parent"> |
23 | 23 | |
24 | - <android.support.v4.app.FragmentPager | |
24 | + <android.support.v4.view.ViewPager | |
25 | 25 | android:id="@+id/pager" |
26 | 26 | android:layout_width="match_parent" |
27 | 27 | android:layout_height="0px" |
28 | 28 | android:layout_weight="1"> |
29 | - </android.support.v4.app.FragmentPager> | |
29 | + </android.support.v4.view.ViewPager> | |
30 | 30 | |
31 | 31 | <LinearLayout android:orientation="horizontal" |
32 | 32 | android:gravity="center" android:measureWithLargestChild="true" |
@@ -81,6 +81,8 @@ | ||
81 | 81 | <string name="first">First</string> |
82 | 82 | <string name="last">Last</string> |
83 | 83 | |
84 | + <string name="fragment_state_pager_support">Fragment/State Pager</string> | |
85 | + | |
84 | 86 | <string name="loader_cursor_support">Loader/Cursor</string> |
85 | 87 | |
86 | 88 | <string name="loader_throttle_support">Loader/Throttle</string> |
@@ -22,8 +22,10 @@ import com.example.android.supportv4.R; | ||
22 | 22 | import android.os.Bundle; |
23 | 23 | import android.support.v4.app.Fragment; |
24 | 24 | import android.support.v4.app.FragmentActivity; |
25 | -import android.support.v4.app.FragmentPager; | |
25 | +import android.support.v4.app.FragmentManager; | |
26 | +import android.support.v4.app.FragmentPagerAdapter; | |
26 | 27 | import android.support.v4.app.ListFragment; |
28 | +import android.support.v4.view.ViewPager; | |
27 | 29 | import android.util.Log; |
28 | 30 | import android.view.LayoutInflater; |
29 | 31 | import android.view.View; |
@@ -34,19 +36,22 @@ import android.widget.Button; | ||
34 | 36 | import android.widget.ListView; |
35 | 37 | import android.widget.TextView; |
36 | 38 | |
37 | -public class FragmentPagerSupport extends FragmentActivity | |
38 | - implements FragmentPager.Adapter { | |
39 | +public class FragmentPagerSupport extends FragmentActivity { | |
39 | 40 | static final int NUM_ITEMS = 10; |
40 | 41 | |
41 | - FragmentPager mPager; | |
42 | + MyAdapter mAdapter; | |
43 | + | |
44 | + ViewPager mPager; | |
42 | 45 | |
43 | 46 | @Override |
44 | 47 | protected void onCreate(Bundle savedInstanceState) { |
45 | 48 | super.onCreate(savedInstanceState); |
46 | 49 | setContentView(R.layout.fragment_pager); |
47 | 50 | |
48 | - mPager = (FragmentPager)findViewById(R.id.pager); | |
49 | - mPager.setAdapter(this); | |
51 | + mAdapter = new MyAdapter(getSupportFragmentManager()); | |
52 | + | |
53 | + mPager = (ViewPager)findViewById(R.id.pager); | |
54 | + mPager.setAdapter(mAdapter); | |
50 | 55 | |
51 | 56 | // Watch for button clicks. |
52 | 57 | Button button = (Button)findViewById(R.id.goto_first); |
@@ -63,14 +68,20 @@ public class FragmentPagerSupport extends FragmentActivity | ||
63 | 68 | }); |
64 | 69 | } |
65 | 70 | |
66 | - @Override | |
67 | - public int getCount() { | |
68 | - return NUM_ITEMS; | |
69 | - } | |
71 | + public static class MyAdapter extends FragmentPagerAdapter { | |
72 | + public MyAdapter(FragmentManager fm) { | |
73 | + super(fm); | |
74 | + } | |
70 | 75 | |
71 | - @Override | |
72 | - public Fragment getItem(int position) { | |
73 | - return ArrayListFragment.newInstance(position); | |
76 | + @Override | |
77 | + public int getCount() { | |
78 | + return NUM_ITEMS; | |
79 | + } | |
80 | + | |
81 | + @Override | |
82 | + public Fragment getItem(int position) { | |
83 | + return ArrayListFragment.newInstance(position); | |
84 | + } | |
74 | 85 | } |
75 | 86 | |
76 | 87 | public static class ArrayListFragment extends ListFragment { |
@@ -0,0 +1,140 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2011 The Android Open Source Project | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | + | |
17 | +package com.example.android.supportv4.app; | |
18 | + | |
19 | +import android.support.v4.app.Fragment; | |
20 | +import android.support.v4.app.FragmentActivity; | |
21 | +import android.support.v4.app.FragmentManager; | |
22 | +import android.support.v4.app.FragmentStatePagerAdapter; | |
23 | +import android.support.v4.app.ListFragment; | |
24 | +import android.support.v4.view.ViewPager; | |
25 | + | |
26 | +import android.os.Bundle; | |
27 | +import android.util.Log; | |
28 | +import android.view.LayoutInflater; | |
29 | +import android.view.View; | |
30 | +import android.view.ViewGroup; | |
31 | +import android.view.View.OnClickListener; | |
32 | +import android.widget.ArrayAdapter; | |
33 | +import android.widget.Button; | |
34 | +import android.widget.ListView; | |
35 | +import android.widget.TextView; | |
36 | + | |
37 | +import com.example.android.supportv4.Cheeses; | |
38 | +import com.example.android.supportv4.R; | |
39 | + | |
40 | +public class FragmentStatePagerSupport extends FragmentActivity { | |
41 | + static final int NUM_ITEMS = 10; | |
42 | + | |
43 | + MyAdapter mAdapter; | |
44 | + | |
45 | + ViewPager mPager; | |
46 | + | |
47 | + @Override | |
48 | + protected void onCreate(Bundle savedInstanceState) { | |
49 | + super.onCreate(savedInstanceState); | |
50 | + setContentView(R.layout.fragment_pager); | |
51 | + | |
52 | + mAdapter = new MyAdapter(getSupportFragmentManager()); | |
53 | + | |
54 | + mPager = (ViewPager)findViewById(R.id.pager); | |
55 | + mPager.setAdapter(mAdapter); | |
56 | + | |
57 | + // Watch for button clicks. | |
58 | + Button button = (Button)findViewById(R.id.goto_first); | |
59 | + button.setOnClickListener(new OnClickListener() { | |
60 | + public void onClick(View v) { | |
61 | + mPager.setCurrentItem(0); | |
62 | + } | |
63 | + }); | |
64 | + button = (Button)findViewById(R.id.goto_last); | |
65 | + button.setOnClickListener(new OnClickListener() { | |
66 | + public void onClick(View v) { | |
67 | + mPager.setCurrentItem(NUM_ITEMS-1); | |
68 | + } | |
69 | + }); | |
70 | + } | |
71 | + | |
72 | + public static class MyAdapter extends FragmentStatePagerAdapter { | |
73 | + public MyAdapter(FragmentManager fm) { | |
74 | + super(fm); | |
75 | + } | |
76 | + | |
77 | + @Override | |
78 | + public int getCount() { | |
79 | + return NUM_ITEMS; | |
80 | + } | |
81 | + | |
82 | + @Override | |
83 | + public Fragment getItem(int position) { | |
84 | + return ArrayListFragment.newInstance(position); | |
85 | + } | |
86 | + } | |
87 | + | |
88 | + public static class ArrayListFragment extends ListFragment { | |
89 | + int mNum; | |
90 | + | |
91 | + /** | |
92 | + * Create a new instance of CountingFragment, providing "num" | |
93 | + * as an argument. | |
94 | + */ | |
95 | + static ArrayListFragment newInstance(int num) { | |
96 | + ArrayListFragment f = new ArrayListFragment(); | |
97 | + | |
98 | + // Supply num input as an argument. | |
99 | + Bundle args = new Bundle(); | |
100 | + args.putInt("num", num); | |
101 | + f.setArguments(args); | |
102 | + | |
103 | + return f; | |
104 | + } | |
105 | + | |
106 | + /** | |
107 | + * When creating, retrieve this instance's number from its arguments. | |
108 | + */ | |
109 | + @Override | |
110 | + public void onCreate(Bundle savedInstanceState) { | |
111 | + super.onCreate(savedInstanceState); | |
112 | + mNum = getArguments() != null ? getArguments().getInt("num") : 1; | |
113 | + } | |
114 | + | |
115 | + /** | |
116 | + * The Fragment's UI is just a simple text view showing its | |
117 | + * instance number. | |
118 | + */ | |
119 | + @Override | |
120 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
121 | + Bundle savedInstanceState) { | |
122 | + View v = inflater.inflate(R.layout.fragment_pager_list, container, false); | |
123 | + View tv = v.findViewById(R.id.text); | |
124 | + ((TextView)tv).setText("Fragment #" + mNum); | |
125 | + return v; | |
126 | + } | |
127 | + | |
128 | + @Override | |
129 | + public void onActivityCreated(Bundle savedInstanceState) { | |
130 | + super.onActivityCreated(savedInstanceState); | |
131 | + setListAdapter(new ArrayAdapter<String>(getActivity(), | |
132 | + android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings)); | |
133 | + } | |
134 | + | |
135 | + @Override | |
136 | + public void onListItemClick(ListView l, View v, int position, long id) { | |
137 | + Log.i("FragmentList", "Item clicked: " + id); | |
138 | + } | |
139 | + } | |
140 | +} |
@@ -63,7 +63,7 @@ public class LoaderThrottleSupport extends FragmentActivity { | ||
63 | 63 | /** |
64 | 64 | * The authority we use to get to our sample provider. |
65 | 65 | */ |
66 | - public static final String AUTHORITY = "com.example.android.apis.support.app.LoaderThrottle"; | |
66 | + public static final String AUTHORITY = "com.example.android.apis.supportv4.app.LoaderThrottle"; | |
67 | 67 | |
68 | 68 | /** |
69 | 69 | * Definition of the contract for the main table of our provider. |
@@ -45,9 +45,15 @@ and loaders.</p> | ||
45 | 45 | <dd>Demonstrates populating custom menu items from a Fragment.</dd> |
46 | 46 | |
47 | 47 | <dt><a href="FragmentPagerSupport.html">Fragment Pager Support</a></dt> |
48 | - <dd>A support class for using the Fragment APIs to build | |
49 | - a user interface where the user can fling left or right to switch | |
50 | - between fragments.</dd> | |
48 | + <dd>Demonstrates the use of the support class ViewPager with a | |
49 | + FragmentPagerAdapter to build a user interface where the user can fling | |
50 | + left or right to switch between fragments.</dd> | |
51 | + | |
52 | + <dt><a href="FragmentStatePagerSupport.html">Fragment State Pager Support</a></dt> | |
53 | + <dd>Demonstrates the use of the support class ViewPager with a | |
54 | + FragmentStatePagerAdapter to build a user interface where the user can fling | |
55 | + left or right to switch between fragments. This versions of the adapter | |
56 | + doesn't keep around the fragment instances that ViewPager has destroyed.</dd> | |
51 | 57 | |
52 | 58 | <dt><a href="FragmentReceiveResultSupport.html">Fragment Receive Result</a></dt> |
53 | 59 | <dd>Demonstrates starting a new Activity from a Fragment, and receiving |