forked from udacity/Android_Me
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
242 lines (217 loc) · 9.14 KB
/
MainActivity.java
File metadata and controls
242 lines (217 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package nl.mahmood.androidmeexercise;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import nl.mahmood.androidmeexercise.data.AndroidImageAssets;
/**
* Pre Requirements
* 1 - create dimens.xml in value folder and in values-w820dp folder
* 2 - copy all images to drawable folder
* do TODO (1) : create 'BodyPartFragment' class
* do TODO (2) : create 'AndroidMeActivity' activity
* do TODO (3) : set 'AndroidMeActivity' as start activity
* do TODO (4) : create activity_android_me.xml
* do TODO (5) : create fragment_body_part.xml
* do TODO (6) : create AndroidImageAssets class in data package and write all code in class body (that's easy)
* do steps 0 to 6 in BodyPartFragment class
* do steps 7 to 11 in AndroidMeActivity Activity
* now you can run project, in AndroidMeActivity appears 3 pictures head and body and leg but nothing change
* do steps 12 to 19 in BodyPartFragment class
* you can run project, images change with click and save the state
* do TODO (7) : create fragment_master_list.xml
* do TODO (8) : create MasterListAdapter class
* do TODO (9) : create MasterListFragment class
* do steps 20 to 23 in MasterListAdapter class
* do steps 24 to 31 in MasterListFragment class
* do steps 32 to 34 in MainActivity class
* do TODO (10) : change the root element to Fragment in activity_main.xml
* do TODO (11) : set 'MainActivity' as start activity
* do steps 34 to 38 in AndroidMeActivity class
* do TODO (12) : change elements in fragment_master_list.xml
* do steps 39 to 43 in MainActivity class
* do TODO (13) : change elements in activity_main.xml
* do steps 44 to 46 in MainActivity class
* do step 47 in BodyPartFragment class
* do step 48 in MasterListAdapter class
*/
/**
* 32 implements MainActivity as MasterListFragment.OnImageClickListener interface
*/
// This activity is responsible for displaying the master list of all images
// Implement the MasterListFragment callback, OnImageClickListener
public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener
{
/**
* 39 add 3 variables
*/
// Variables to store the values for the list index of the selected images
// The default value will be index = 0
private int headIndex;
private int bodyIndex;
private int legIndex;
/**
* 44 add a boolean variable
*/
// Track whether to display a two-pane or single-pane UI
// A single-pane display refers to phone screens, and two-pane to larger tablet screens
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 45 start
*/
// Determine if you're creating a two-pane or single-pane display
if (findViewById(R.id.android_me_linear_layout) != null) {
// This LinearLayout will only initially exist in the two-pane tablet case
mTwoPane = true;
// Change the GridView to space out the images more on tablet
GridView gridView = (GridView) findViewById(R.id.images_grid_view);
gridView.setNumColumns(2);
// Getting rid of the "Next" button that appears on phones for launching a separate activity
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setVisibility(View.GONE);
if (savedInstanceState == null) {
// In two-pane mode, add initial BodyPartFragments to the screen
FragmentManager fragmentManager = getSupportFragmentManager();
// Creating a new head fragment
BodyPartFragment headFragment = new BodyPartFragment();
headFragment.setImageIds(AndroidImageAssets.getHeads());
// Add the fragment to its container using a transaction
fragmentManager.beginTransaction()
.add(R.id.head_container, headFragment)
.commit();
// New body fragment
BodyPartFragment bodyFragment = new BodyPartFragment();
bodyFragment.setImageIds(AndroidImageAssets.getBodies());
fragmentManager.beginTransaction()
.add(R.id.body_container, bodyFragment)
.commit();
// New leg fragment
BodyPartFragment legFragment = new BodyPartFragment();
legFragment.setImageIds(AndroidImageAssets.getLegs());
fragmentManager.beginTransaction()
.add(R.id.leg_container, legFragment)
.commit();
}
} else {
// We're in single-pane mode and displaying fragments on a phone in separate activities
mTwoPane = false;
}
/**
* 45 end
*/
}
/**
* 33 implement method
*/
// Define the behavior for onImageSelected
@Override
public void onImageSelected(int position)
{
/**
* 34
*/
// Create a Toast that displays the position that was clicked
Toast.makeText(this, "Position clicked = " + position, Toast.LENGTH_SHORT).show();
/**
* 39
*/
// Based on where a user has clicked, store the selected list index for the head, body, and leg BodyPartFragments
// bodyPartNumber will be = 0 for the head fragment, 1 for the body, and 2 for the leg fragment
// Dividing by 12 gives us these integer values because each list of images resources has a size of 12
int bodyPartNumber = position / 12;
// Store the correct list index no matter where in the image list has been clicked
// This ensures that the index will always be a value between 0-11
int listIndex = position - 12 * bodyPartNumber;
/**
* 46
*/
// Handle the two-pane case and replace existing fragments right when a new image is selected from the master list
if (mTwoPane) {
// Create two=pane interaction
BodyPartFragment newFragment = new BodyPartFragment();
// Set the currently displayed item for the correct body part fragment
switch (bodyPartNumber) {
case 0:
// A head image has been clicked
// Give the correct image resources to the new fragment
newFragment.setImageIds(AndroidImageAssets.getHeads());
newFragment.setListIndex(listIndex);
// Replace the old head fragment with a new one
getSupportFragmentManager().beginTransaction()
.replace(R.id.head_container, newFragment)
.commit();
break;
case 1:
newFragment.setImageIds(AndroidImageAssets.getBodies());
newFragment.setListIndex(listIndex);
getSupportFragmentManager().beginTransaction()
.replace(R.id.body_container, newFragment)
.commit();
break;
case 2:
newFragment.setImageIds(AndroidImageAssets.getLegs());
newFragment.setListIndex(listIndex);
getSupportFragmentManager().beginTransaction()
.replace(R.id.leg_container, newFragment)
.commit();
break;
default:
break;
}
} else {
/**
* 40
*/
// Set the currently displayed item for the correct body part fragment
switch (bodyPartNumber) {
case 0:
headIndex = listIndex;
break;
case 1:
bodyIndex = listIndex;
break;
case 2:
legIndex = listIndex;
break;
default:
break;
}
}
/**
* 41
*/
// Put this information in a Bundle and attach it to an Intent that will launch an AndroidMeActivity
Bundle b = new Bundle();
b.putInt("headIndex", headIndex);
b.putInt("bodyIndex", bodyIndex);
b.putInt("legIndex", legIndex);
/**
* 42
*/
// Attach the Bundle to an intent
final Intent intent = new Intent(this, AndroidMeActivity.class);
intent.putExtras(b);
/**
* 43
*/
// The "Next" button launches a new AndroidMeActivity
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
startActivity(intent);
}
});
}
}