Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit 6d7b7be

Browse files
cezannecAsser
cezannec
authored and
Asser
committed
TFragments.02-Solution-DisplayThreeFragments
1 parent 29da6c2 commit 6d7b7be

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.os.Bundle;
2222

2323
import com.example.android.android_me.R;
24+
import com.example.android.android_me.data.AndroidImageAssets;
2425

2526
// This activity will display a custom Android image composed of three body parts: head, body, and legs
2627
public class AndroidMeActivity extends AppCompatActivity {
@@ -34,7 +35,9 @@ protected void onCreate(Bundle savedInstanceState) {
3435
// Create a new head BodyPartFragment
3536
BodyPartFragment headFragment = new BodyPartFragment();
3637

37-
// TODO (4) Set the list of image id's for the head fragment and set the position to the second image in the list
38+
// Set the list of image id's for the head fragment and set the position to the second image in the list
39+
headFragment.setImageIds(AndroidImageAssets.getHeads());
40+
headFragment.setListIndex(1);
3841

3942
// Add the fragment to its container using a FragmentManager and a Transaction
4043
FragmentManager fragmentManager = getSupportFragmentManager();
@@ -43,7 +46,20 @@ protected void onCreate(Bundle savedInstanceState) {
4346
.add(R.id.head_container, headFragment)
4447
.commit();
4548

46-
// TODO (5) Create and display the body and leg BodyPartFragments
49+
// Create and display the body and leg BodyPartFragments
50+
51+
BodyPartFragment bodyFragment = new BodyPartFragment();
52+
bodyFragment.setImageIds(AndroidImageAssets.getBodies());
53+
fragmentManager.beginTransaction()
54+
.add(R.id.body_container, bodyFragment)
55+
.commit();
56+
57+
BodyPartFragment legFragment = new BodyPartFragment();
58+
legFragment.setImageIds(AndroidImageAssets.getLegs());
59+
fragmentManager.beginTransaction()
60+
.add(R.id.leg_container, legFragment)
61+
.commit();
62+
4763

4864
}
4965
}

app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java

+26-8
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@
1818

1919
import android.os.Bundle;
2020
import android.support.v4.app.Fragment;
21+
import android.util.Log;
2122
import android.view.LayoutInflater;
2223
import android.view.View;
2324
import android.view.ViewGroup;
2425
import android.widget.ImageView;
2526

2627
import com.example.android.android_me.R;
27-
import com.example.android.android_me.data.AndroidImageAssets;
28+
29+
import java.util.List;
2830

2931
public class BodyPartFragment extends Fragment {
3032

31-
// TODO (1) Create a setter method and class variable to set and store of a list of image resources
33+
// Tag for logging
34+
private static final String TAG = "BodyPartFragment";
3235

33-
// TODO (2) Create another setter method and variable to track and set the index of the list item to display
34-
// ex. index = 0 is the first image id in the given list , index 1 is the second, and so on
36+
// Variables to store a list of image resources and the index of the image that this fragment displays
37+
private List<Integer> mImageIds;
38+
private int mListIndex;
3539

3640
/**
3741
* Mandatory empty constructor for the fragment manager to instantiate the fragment
@@ -51,14 +55,28 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5155
// Get a reference to the ImageView in the fragment layout
5256
ImageView imageView = (ImageView) rootView.findViewById(R.id.body_part_image_view);
5357

54-
// Set the image to the first in our list of head images
55-
imageView.setImageResource(AndroidImageAssets.getHeads().get(0));
56-
57-
// TODO (3) If a list of image ids exists, set the image resource to the correct item in that list
58+
// If a list of image ids exists, set the image resource to the correct item in that list
5859
// Otherwise, create a Log statement that indicates that the list was not found
60+
if(mImageIds != null){
61+
// Set the image resource to the list item at the stored index
62+
imageView.setImageResource(mImageIds.get(mListIndex));
63+
} else {
64+
Log.v(TAG, "This fragment has a null list of image id's");
65+
}
5966

6067
// Return the rootView
6168
return rootView;
6269
}
6370

71+
// Setter methods for keeping track of the list images this fragment can display and which image
72+
// in the list is currently being displayed
73+
74+
public void setImageIds(List<Integer> imageIds) {
75+
mImageIds = imageIds;
76+
}
77+
78+
public void setListIndex(int index) {
79+
mListIndex = index;
80+
}
81+
6482
}

app/src/main/res/layout/activity_android_me.xml

+11
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,23 @@
3030
android:paddingTop="@dimen/activity_vertical_margin">
3131

3232

33+
<!-- Three containers for each Android-Me body part -->
3334
<!-- This container holds the head BodyPartFragment of the custom Android-Me image -->
3435
<FrameLayout android:id="@+id/head_container"
3536
android:layout_width="match_parent"
3637
android:layout_height="180dp"
3738
android:scaleType="centerInside"/>
3839

40+
<!-- The remaining containers for the body and leg BodyPartFragments -->
41+
<FrameLayout android:id="@+id/body_container"
42+
android:layout_width="match_parent"
43+
android:layout_height="180dp"
44+
android:scaleType="centerInside"/>
45+
46+
<FrameLayout android:id="@+id/leg_container"
47+
android:layout_width="match_parent"
48+
android:layout_height="180dp"
49+
android:scaleType="centerInside"/>
3950

4051

4152
</LinearLayout>

0 commit comments

Comments
 (0)