2
2
3
3
import android .app .*;
4
4
import android .content .Context ;
5
+ import android .content .Intent ;
5
6
import android .os .Bundle ;
6
7
import android .support .v13 .app .FragmentPagerAdapter ;
7
8
import android .support .v4 .view .ViewPager ;
9
+ import android .util .Log ;
8
10
import android .view .*;
11
+ import android .widget .AdapterView ;
9
12
import android .widget .GridView ;
10
13
11
14
public class MainActivity extends Activity implements ActionBar .TabListener {
@@ -25,18 +28,20 @@ public class MainActivity extends Activity implements ActionBar.TabListener {
25
28
*/
26
29
ViewPager mViewPager ;
27
30
28
- private static final String TAG = "r3ader" ;
31
+
32
+ //ViewPager.setOffscreenPageLimit(int) way to control how many pages
33
+
34
+ private static final String TAG = MainActivity .class .getName ();
35
+ private String [] sections ;
29
36
30
37
@ Override
31
38
protected void onCreate (Bundle savedInstanceState ) {
32
39
super .onCreate (savedInstanceState );
33
40
setContentView (R .layout .activity_main );
34
-
35
- // Set up the action bar.
41
+ sections = getResources ().getStringArray (R .array .sections );
36
42
final ActionBar actionBar = getActionBar ();
37
43
actionBar .setNavigationMode (ActionBar .NAVIGATION_MODE_TABS );
38
44
39
- int sections = 4 ;
40
45
// Create the adapter that will return a fragment for each of the three
41
46
// primary sections of the activity.
42
47
mSectionsPagerAdapter = new SectionsPagerAdapter (getFragmentManager (), this , sections );
@@ -54,13 +59,20 @@ public void onPageSelected(int position) {
54
59
actionBar .setSelectedNavigationItem (position );
55
60
}
56
61
});
57
- String [] sectionNames = {"In Theaters" , "DVD Releases" , "Opening" , "Box Office" };
58
- for (int i = 0 ; i < sections ; i ++) {
59
- actionBar .addTab (actionBar .newTab ().setText (sectionNames [i ]).setTabListener (this ));
62
+ for (String section : sections ) {
63
+ actionBar .addTab (actionBar .newTab ().setText (getName (section )).setTabListener (this ));
60
64
}
61
65
}
62
66
63
67
68
+ /* String name in resource files can not have spaces,
69
+ hence replace the _ with spaces to get the actual desired
70
+ values.
71
+ */
72
+ public String getName (String withUnderscores ) {
73
+ return withUnderscores .replaceAll ("_" , " " );
74
+ }
75
+
64
76
@ Override
65
77
public boolean onCreateOptionsMenu (Menu menu ) {
66
78
@@ -103,15 +115,9 @@ public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTrans
103
115
public class SectionsPagerAdapter extends FragmentPagerAdapter {
104
116
105
117
private Context context ;
106
- private int sections ;
107
- private String [] LIST = {
108
- "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=24&page=1&country=us&apikey=" ,
109
- "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?page_limit=24&page=1&country=us&apikey=" ,
110
- "http://api.rottentomatoes.com/api/public/v1.0/lists/dvd/upcoming.json?page_limit=24&page=1&country=us&apikey=" ,
111
- "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?page_limit=24&page=1&country=us&apikey="
112
- };
113
-
114
- public SectionsPagerAdapter (FragmentManager fm , Context context , int sections ) {
118
+ private String [] sections ;
119
+
120
+ public SectionsPagerAdapter (FragmentManager fm , Context context , String [] sections ) {
115
121
super (fm );
116
122
this .context = context ;
117
123
this .sections = sections ;
@@ -121,54 +127,74 @@ public SectionsPagerAdapter(FragmentManager fm, Context context, int sections) {
121
127
public Fragment getItem (int position ) {
122
128
// getItem is called to instantiate the fragment for the given page.
123
129
// Return a PlaceholderFragment (defined as a static inner class below).
124
- GridAdapter gridAdapter = new GridAdapter (this .context , LIST [position ]);
125
- return PlaceholderFragment .newInstance (position , context , gridAdapter );
130
+ return PlaceholderFragment .newInstance (position , context );
126
131
}
127
132
128
133
@ Override
129
134
public int getCount () {
130
135
// Show 3 total pages.
131
- return sections ;
136
+ return sections . length ;
132
137
}
133
138
}
134
139
135
140
/**
136
141
* A placeholder fragment containing a simple view.
137
142
*/
138
143
public static class PlaceholderFragment extends Fragment {
139
- /**
140
- * The fragment argument representing the section number for this
141
- * fragment.
142
- */
143
- private static final String ARG_SECTION_NUMBER = "section_number" ;
144
-
145
144
private int position ;
146
- private GridAdapter gridAdapter ;
145
+ private static String [] sections ;
147
146
148
147
/**
149
148
* Returns a new instance of this fragment for the given section
150
149
* number.
151
150
*/
152
- public static PlaceholderFragment newInstance (int sectionNumber , Context context , GridAdapter gridAdapter ) {
151
+ public static PlaceholderFragment newInstance (int sectionNumber , Context context ) {
153
152
PlaceholderFragment fragment = new PlaceholderFragment ();
154
153
Bundle args = new Bundle ();
155
- args .putInt (ARG_SECTION_NUMBER , sectionNumber );
156
- fragment .position = sectionNumber ;
154
+ args .putInt ("position" , sectionNumber );
157
155
fragment .setArguments (args );
158
- fragment .gridAdapter = gridAdapter ;
156
+ fragment .sections = context .getResources ().getStringArray (R .array .sections );
157
+ fragment .position = sectionNumber ;
158
+ Log .d (TAG , "creating a new fragment" );
159
159
return fragment ;
160
160
}
161
161
162
- public PlaceholderFragment () {
163
- }
164
-
165
162
@ Override
166
163
public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
164
+ int labelId = getResources ().getIdentifier (sections [position ], "string" , getActivity ().getPackageName ());
165
+ String url = getResources ().getString (labelId );
166
+ String KEY = getString (R .string .ROTTEN_KEY );
167
+ GridAdapter gridAdapter = new GridAdapter (this .getActivity (), url + KEY );
168
+
169
+
167
170
View rootView = inflater .inflate (R .layout .fragment_main , container , false );
168
171
GridView gridview = (GridView ) rootView .findViewById (R .id .gridView );
172
+ Log .w (TAG ,gridAdapter .toString ());
169
173
gridview .setAdapter (gridAdapter );
174
+ gridview .setOnItemClickListener ((AdapterView <?> parent , View v , int position , long id ) -> {
175
+
176
+ Movie movie = gridAdapter .getItem (position );
177
+ Intent intent = new Intent (this .getActivity (), MovieActivity .class );
178
+ intent .putExtra ("movie" , movie );
179
+ this .getActivity ().startActivity (intent );
180
+ });
181
+ Log .d (TAG , "creating new grid" );
170
182
return rootView ;
171
183
}
184
+
185
+ @ Override
186
+ public void onCreate (Bundle savedInstanceState ) {
187
+ super .onCreate (savedInstanceState );
188
+ if (getArguments () != null ) {
189
+ position = getArguments ().getInt ("position" );
190
+ }
191
+ }
192
+
193
+ @ Override
194
+ public void onDestroy () {
195
+ super .onDestroy ();
196
+ Log .w (TAG ,"Destroying" );
197
+ }
172
198
}
173
199
174
200
}
0 commit comments