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

Compleated 2nd Exercise : Display all Body Part's #53

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 @@ -33,8 +34,18 @@ protected void onCreate(Bundle savedInstanceState) {

// Create a new head BodyPartFragment
BodyPartFragment headFragment = new BodyPartFragment();
BodyPartFragment bodyFragment = new BodyPartFragment();
BodyPartFragment legFragment = 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
headFragment.setmImageIds(AndroidImageAssets.getHeads());
headFragment.setmListIndex(2);

bodyFragment.setmImageIds(AndroidImageAssets.getBodies());
bodyFragment.setmListIndex(2);

legFragment.setmImageIds(AndroidImageAssets.getLegs());
legFragment.setmListIndex(2);

// Add the fragment to its container using a FragmentManager and a Transaction
FragmentManager fragmentManager = getSupportFragmentManager();
Expand All @@ -44,6 +55,12 @@ protected void onCreate(Bundle savedInstanceState) {
.commit();

// TODO (5) Create and display the body and leg BodyPartFragments
fragmentManager.beginTransaction()
.add(R.id.body_container, bodyFragment)
.commit();

fragmentManager.beginTransaction()
.add(R.id.leg_container, legFragment)
.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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;
Expand All @@ -26,7 +27,12 @@
import com.example.android.android_me.R;
import com.example.android.android_me.data.AndroidImageAssets;

import java.util.List;

public class BodyPartFragment extends Fragment {
private static String TAG = "BodypartFragment";
private List<Integer> mImageIds;
private int mListIndex;

// TODO (1) Create a setter method and class variable to set and store of a list of image resources

Expand Down Expand Up @@ -56,9 +62,21 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

// TODO (3) 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){
imageView.setImageResource(mImageIds.get(mListIndex));
}else {
Log.v(TAG, "This Fragment has a null list of image id's");
}

// Return the rootView
return rootView;
}

public void setmImageIds(List<Integer> mImageIds) {
this.mImageIds = mImageIds;
}

public void setmListIndex(int mListIndex) {
this.mListIndex = mListIndex;
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_android_me.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
android:layout_height="180dp"
android:scaleType="centerInside"/>

<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