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

T fragments.03 exercise clicks and saving state #48

Open
wants to merge 5 commits into
base: TFragments.00-StartingCode
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 @@ -77,7 +77,7 @@ public class AndroidImageAssets {
addAll(bodies);
addAll(legs);
}};


// Getter methods that return lists of all head images, body images, and leg images

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,52 @@

package com.example.android.android_me.ui;

import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
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 {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_me);

// TODO (5) Only create new fragments when there is no previously saved state

// Create a new head BodyPartFragment
BodyPartFragment headFragment = new BodyPartFragment();

// 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();

fragmentManager.beginTransaction()
.add(R.id.head_container, headFragment)
.commit();

// 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
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.android_me.ui;

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 java.util.List;

public class BodyPartFragment extends Fragment {

// TODO (3) Create final Strings to store state information about the list of images and list index

// Tag for logging
private static final String TAG = "BodyPartFragment";

// 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
*/
public BodyPartFragment() {
}

/**
* Inflates the fragment layout file and sets the correct resource for the image to display
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the Android-Me fragment layout
View rootView = inflater.inflate(R.layout.fragment_body_part, container, false);

// Get a reference to the ImageView in the fragment layout
ImageView imageView = (ImageView) rootView.findViewById(R.id.body_part_image_view);

// 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));

// TODO (1) Set a click listener on the image view and on a click increment the list index and set the image resource
// TODO (2) If you reach the end of a list of images, set the list index back to 0 (the first item in the list)

} 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;
}

// TODO (4) Override onSaveInstanceState and save the current state of this fragment
}
19 changes: 19 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,6 +30,25 @@
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>

</ScrollView>
20 changes: 20 additions & 0 deletions app/src/main/res/layout/fragment_body_part.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.-->

<!-- This fragment layout displays just one image of an Android-Me body part -->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/body_part_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ImageView>