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

TFragments.02-Solution-DisplayThreeFragments #18

Open
wants to merge 1 commit into
base: TFragments.02-Exercise-DisplayThreeFragments
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.os.Bundle;

import com.example.android.android_me.R;
import com.example.android.android_me.data.AndroidImageAssets;

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

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

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

// TODO (5) Create and display the body and leg BodyPartFragments
// Create and display the body and leg BodyPartFragments

BodyPartFragment bodyFragment = new BodyPartFragment();
bodyFragment.setImageIds(AndroidImageAssets.getBodies());
fragmentManager.beginTransaction()
.add(R.id.body_container, bodyFragment)
.commit();

BodyPartFragment legFragment = new BodyPartFragment();
legFragment.setImageIds(AndroidImageAssets.getLegs());
fragmentManager.beginTransaction()
.add(R.id.leg_container, legFragment)
.commit();


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.android.android_me.R;
import com.example.android.android_me.data.AndroidImageAssets;

import java.util.List;

public class BodyPartFragment extends Fragment {

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

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

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

// Set the image to the first in our list of head images
imageView.setImageResource(AndroidImageAssets.getHeads().get(0));

// TODO (3) If a list of image ids exists, set the image resource to the correct item in that list
// If a list of image ids exists, set the image resource to the correct item in that list
// Otherwise, create a Log statement that indicates that the list was not found
if(mImageIds != null){
// Set the image resource to the list item at the stored index
imageView.setImageResource(mImageIds.get(mListIndex));
} else {
Log.v(TAG, "This fragment has a null list of image id's");
}

// Return the rootView
return rootView;
}

// Setter methods for keeping track of the list images this fragment can display and which image
// in the list is currently being displayed

public void setImageIds(List<Integer> imageIds) {
mImageIds = imageIds;
}

public void setListIndex(int index) {
mListIndex = index;
}

}
11 changes: 11 additions & 0 deletions app/src/main/res/layout/activity_android_me.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@
android:paddingTop="@dimen/activity_vertical_margin">


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

<!-- The remaining containers for the body and leg BodyPartFragments -->
<FrameLayout android:id="@+id/body_container"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside"/>

<FrameLayout android:id="@+id/leg_container"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside"/>


</LinearLayout>
Expand Down