From 9636797838d0bf35e0346945965514d834e7ed09 Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 17:47:39 -0800 Subject: [PATCH 1/9] TFragments.01-Solution-CreateBodyPartFragment --- .../android_me/ui/AndroidMeActivity.java | 20 +++---- .../android_me/ui/BodyPartFragment.java | 56 +++++++++++++++++++ .../main/res/layout/activity_android_me.xml | 8 ++- .../main/res/layout/fragment_body_part.xml | 20 +++++++ 4 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java create mode 100644 app/src/main/res/layout/fragment_body_part.xml diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index 7e5d243b0..db6f37c88 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -16,6 +16,7 @@ package com.example.android.android_me.ui; +import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; @@ -24,21 +25,20 @@ // This activity will display a custom Android image composed of three body parts: head, body, and legs public class AndroidMeActivity extends AppCompatActivity { - // TODO (1) Create a layout file that displays one body part image named fragment_body_part.xml - // This layout should contain a single ImageView - - // TODO (2) Create a new class called BodyPartFragment to display an image of an Android-Me body part - // In this class, you'll need to implement an empty constructor and the onCreateView method - // TODO (3) Show the first image in the list of head images - // Soon, you'll update this image display code to show any image you want - - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_me); - // TODO (5) Create a new BodyPartFragment instance and display it using the FragmentManager + // Create a new head BodyPartFragment + BodyPartFragment headFragment = new BodyPartFragment(); + + // Add the fragment to its container using a FragmentManager and a Transaction + FragmentManager fragmentManager = getSupportFragmentManager(); + + fragmentManager.beginTransaction() + .add(R.id.head_container, headFragment) + .commit(); } } diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java new file mode 100644 index 000000000..bc065113c --- /dev/null +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -0,0 +1,56 @@ +/* +* 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.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; + +public class BodyPartFragment extends Fragment { + + /** + * 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); + + // Set the image to the first in our list of head images + imageView.setImageResource(AndroidImageAssets.getHeads().get(0)); + + // Return the rootView + return rootView; + } + +} diff --git a/app/src/main/res/layout/activity_android_me.xml b/app/src/main/res/layout/activity_android_me.xml index 71bd30adb..b7af0db2c 100644 --- a/app/src/main/res/layout/activity_android_me.xml +++ b/app/src/main/res/layout/activity_android_me.xml @@ -29,8 +29,12 @@ android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> - - + + + diff --git a/app/src/main/res/layout/fragment_body_part.xml b/app/src/main/res/layout/fragment_body_part.xml new file mode 100644 index 000000000..1455ea00b --- /dev/null +++ b/app/src/main/res/layout/fragment_body_part.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file From 29da6c23a1414659a228aebb5db7948ffaf9cb06 Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 19:56:18 -0800 Subject: [PATCH 2/9] TFragments.02-Exercise-DisplayThreeFragments --- .../example/android/android_me/ui/AndroidMeActivity.java | 5 +++++ .../example/android/android_me/ui/BodyPartFragment.java | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index db6f37c88..5d98c17bc 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -34,11 +34,16 @@ 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 + // Add the fragment to its container using a FragmentManager and a Transaction FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .add(R.id.head_container, headFragment) .commit(); + + // TODO (5) Create and display the body and leg BodyPartFragments + } } diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java index bc065113c..cdb944a3a 100644 --- a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -28,6 +28,11 @@ public class BodyPartFragment extends Fragment { + // TODO (1) Create a setter method and class variable to set and store of a list of image resources + + // 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 + /** * Mandatory empty constructor for the fragment manager to instantiate the fragment */ @@ -49,6 +54,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa // 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 + // Otherwise, create a Log statement that indicates that the list was not found + // Return the rootView return rootView; } From 6d7b7bec54a5230c2b301c924a5652551db6854b Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 21:20:12 -0800 Subject: [PATCH 3/9] TFragments.02-Solution-DisplayThreeFragments --- .../android_me/ui/AndroidMeActivity.java | 20 +++++++++-- .../android_me/ui/BodyPartFragment.java | 34 ++++++++++++++----- .../main/res/layout/activity_android_me.xml | 11 ++++++ 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index 5d98c17bc..6ba47a389 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -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 { @@ -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(); @@ -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(); + } } diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java index cdb944a3a..0526e957f 100644 --- a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -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 mImageIds; + private int mListIndex; /** * Mandatory empty constructor for the fragment manager to instantiate the fragment @@ -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 imageIds) { + mImageIds = imageIds; + } + + public void setListIndex(int index) { + mListIndex = index; + } + } diff --git a/app/src/main/res/layout/activity_android_me.xml b/app/src/main/res/layout/activity_android_me.xml index b7af0db2c..c65bc721b 100644 --- a/app/src/main/res/layout/activity_android_me.xml +++ b/app/src/main/res/layout/activity_android_me.xml @@ -30,12 +30,23 @@ android:paddingTop="@dimen/activity_vertical_margin"> + + + + + From c245127a728896cd81e57c97e241ae6225a1d76f Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 21:44:45 -0800 Subject: [PATCH 4/9] TFragments.03-Exercise-ClicksAndSavingState --- .../example/android/android_me/ui/AndroidMeActivity.java | 2 ++ .../example/android/android_me/ui/BodyPartFragment.java | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index 6ba47a389..39345cafb 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -32,6 +32,8 @@ 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(); diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java index 0526e957f..370ba872d 100644 --- a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -30,6 +30,8 @@ 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"; @@ -60,6 +62,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa 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"); } @@ -79,4 +85,5 @@ public void setListIndex(int index) { mListIndex = index; } + // TODO (4) Override onSaveInstanceState and save the current state of this fragment } From 7e5676bde5f3c128656ad5c339fd8cff68d4afce Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 22:02:06 -0800 Subject: [PATCH 5/9] TFragments.03-Solution-ClicksAndSavingState --- .../android_me/ui/AndroidMeActivity.java | 47 ++++++++++--------- .../android_me/ui/BodyPartFragment.java | 41 ++++++++++++++-- 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index 39345cafb..dd0a6ba78 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -32,36 +32,37 @@ 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 + // Only create new fragments when there is no previously saved state + if(savedInstanceState == null) { - // Create a new head BodyPartFragment - BodyPartFragment headFragment = new BodyPartFragment(); + // 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); + // 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(); + // Add the fragment to its container using a FragmentManager and a Transaction + FragmentManager fragmentManager = getSupportFragmentManager(); - fragmentManager.beginTransaction() - .add(R.id.head_container, headFragment) - .commit(); + fragmentManager.beginTransaction() + .add(R.id.head_container, headFragment) + .commit(); - // 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(); + 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(); + } } } diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java index 370ba872d..225bdb061 100644 --- a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -26,11 +26,14 @@ import com.example.android.android_me.R; +import java.util.ArrayList; 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 + // Final Strings to store state information about the list of images and list index + public static final String IMAGE_ID_LIST = "image_ids"; + public static final String LIST_INDEX = "list_index"; // Tag for logging private static final String TAG = "BodyPartFragment"; @@ -51,11 +54,17 @@ public BodyPartFragment() { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + // Load the saved state (the list of images and list index) if there is one + if(savedInstanceState != null) { + mImageIds = savedInstanceState.getIntegerArrayList(IMAGE_ID_LIST); + mListIndex = savedInstanceState.getInt(LIST_INDEX); + } + // 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); + final 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 @@ -63,8 +72,21 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa // 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) + // Set a click listener on the image view + imageView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + // Increment position as long as the index remains <= the size of the image ids list + if(mListIndex < mImageIds.size()-1) { + mListIndex++; + } else { + // The end of list has been reached, so return to beginning index + mListIndex = 0; + } + // Set the image resource to the new list item + imageView.setImageResource(mImageIds.get(mListIndex)); + } + }); } else { Log.v(TAG, "This fragment has a null list of image id's"); @@ -85,5 +107,14 @@ public void setListIndex(int index) { mListIndex = index; } - // TODO (4) Override onSaveInstanceState and save the current state of this fragment + /** + * Save the current state of this fragment + */ + @Override + public void onSaveInstanceState(Bundle currentState) { + currentState.putIntegerArrayList(IMAGE_ID_LIST, (ArrayList) mImageIds); + currentState.putInt(LIST_INDEX, mListIndex); + } + + } From 8a48a578bbf33d3eac044b6e46463882de5dd31d Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 22:15:21 -0800 Subject: [PATCH 6/9] TFragments.04-Exercise-CreateMasterListFragment --- app/src/main/AndroidManifest.xml | 3 +++ .../android/android_me/ui/AndroidMeActivity.java | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c9a63ea85..a4bb27364 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,9 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> + + + diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index dd0a6ba78..ba552f613 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -26,6 +26,19 @@ // This activity will display a custom Android image composed of three body parts: head, body, and legs public class AndroidMeActivity extends AppCompatActivity { + // TODO (1) Create a fragment_master_list.xml layout file to display all our images; this should be a GridView + + // TODO (2) Create a new class called MasterListFragment which will display the GridView list of ALL AndroidMe images + // In the fragment class, you'll need to implement an empty constructor, and onCreateView + + // TODO (3) In the MasterListFragment class, create a new MasterListAdapter and set it on the GridView + // The MasterListAdapter code is provided; it creates the ImageViews that are contained in the GridView + // The adapter takes as parameters (Context context, List imageIds) + + // After creating the fragment.. + // TODO (4) Create a new Activity named MainActivity and a corresponding layout file that displays a MasterListFragment + // Remember, to display a static fragment in a layout file, use the tag + @Override protected void onCreate(Bundle savedInstanceState) { From 2f1f6231e1a7714caefea188b5986d18e918763f Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 22:34:45 -0800 Subject: [PATCH 7/9] TFragments.04-Solution-CreateMasterListFragment --- app/src/main/AndroidManifest.xml | 8 ++- .../android_me/ui/AndroidMeActivity.java | 13 ---- .../android/android_me/ui/MainActivity.java | 35 +++++++++++ .../android_me/ui/MasterListFragment.java | 59 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 19 ++++++ .../main/res/layout/fragment_master_list.xml | 27 +++++++++ 6 files changed, 145 insertions(+), 16 deletions(-) create mode 100755 app/src/main/java/com/example/android/android_me/ui/MainActivity.java create mode 100755 app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java create mode 100755 app/src/main/res/layout/activity_main.xml create mode 100755 app/src/main/res/layout/fragment_master_list.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a4bb27364..c68dcaa01 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,15 +9,17 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> - - - + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index ba552f613..dd0a6ba78 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -26,19 +26,6 @@ // This activity will display a custom Android image composed of three body parts: head, body, and legs public class AndroidMeActivity extends AppCompatActivity { - // TODO (1) Create a fragment_master_list.xml layout file to display all our images; this should be a GridView - - // TODO (2) Create a new class called MasterListFragment which will display the GridView list of ALL AndroidMe images - // In the fragment class, you'll need to implement an empty constructor, and onCreateView - - // TODO (3) In the MasterListFragment class, create a new MasterListAdapter and set it on the GridView - // The MasterListAdapter code is provided; it creates the ImageViews that are contained in the GridView - // The adapter takes as parameters (Context context, List imageIds) - - // After creating the fragment.. - // TODO (4) Create a new Activity named MainActivity and a corresponding layout file that displays a MasterListFragment - // Remember, to display a static fragment in a layout file, use the tag - @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/app/src/main/java/com/example/android/android_me/ui/MainActivity.java b/app/src/main/java/com/example/android/android_me/ui/MainActivity.java new file mode 100755 index 000000000..d854d5c3f --- /dev/null +++ b/app/src/main/java/com/example/android/android_me/ui/MainActivity.java @@ -0,0 +1,35 @@ +/* +* 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.v7.app.AppCompatActivity; + +import com.example.android.android_me.R; + +// This activity is responsible for displaying the master list of all images +public class MainActivity extends AppCompatActivity { + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + } + +} diff --git a/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java b/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java new file mode 100755 index 000000000..7c9f210ca --- /dev/null +++ b/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java @@ -0,0 +1,59 @@ +/* +* 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.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.GridView; + +import com.example.android.android_me.R; +import com.example.android.android_me.data.AndroidImageAssets; + + +// This fragment displays all of the AndroidMe images in one large list +// The list appears as a grid of images +public class MasterListFragment extends Fragment { + + // Mandatory empty constructor + public MasterListFragment() { + } + + // Inflates the GridView of all AndroidMe images + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + final View rootView = inflater.inflate(R.layout.fragment_master_list, container, false); + + // Get a reference to the GridView in the fragment_master_list xml layout file + GridView gridView = (GridView) rootView.findViewById(R.id.images_grid_view); + + // Create the adapter + // This adapter takes in the context and an ArrayList of ALL the image resources to display + MasterListAdapter mAdapter = new MasterListAdapter(getContext(), AndroidImageAssets.getAll()); + + // Set the adapter on the GridView + gridView.setAdapter(mAdapter); + + // Return the root view + return rootView; + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100755 index 000000000..70ace7801 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,19 @@ + + + + + diff --git a/app/src/main/res/layout/fragment_master_list.xml b/app/src/main/res/layout/fragment_master_list.xml new file mode 100755 index 000000000..af422d9e0 --- /dev/null +++ b/app/src/main/res/layout/fragment_master_list.xml @@ -0,0 +1,27 @@ + + + + + + From 2e2ff044891afb85d4482d3aa96761c0fb5905d2 Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 23:22:41 -0800 Subject: [PATCH 8/9] TFragments.05-Exercise-InterfaceCommunication --- .../com/example/android/android_me/ui/MainActivity.java | 3 +++ .../android/android_me/ui/MasterListFragment.java | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/app/src/main/java/com/example/android/android_me/ui/MainActivity.java b/app/src/main/java/com/example/android/android_me/ui/MainActivity.java index d854d5c3f..0a1aa44bb 100755 --- a/app/src/main/java/com/example/android/android_me/ui/MainActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/MainActivity.java @@ -22,6 +22,7 @@ import com.example.android.android_me.R; // This activity is responsible for displaying the master list of all images +// TODO (4) Implement the MasterListFragment callback, OnImageClickListener public class MainActivity extends AppCompatActivity { @@ -32,4 +33,6 @@ protected void onCreate(Bundle savedInstanceState) { } + // TODO (5) Define the behavior for onImageSelected; create a Toast that displays the position clicked + } diff --git a/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java b/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java index 7c9f210ca..0c7e2404c 100755 --- a/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java @@ -31,6 +31,13 @@ // The list appears as a grid of images public class MasterListFragment extends Fragment { + // TODO (1) Define a new interface OnImageClickListener that triggers a callback in the host activity + // The callback is a method named onImageSelected(int position) that contains information about + // which position on the grid of images a user has clicked + + // TODO (2) Override onAttach to make sure that the container activity has implemented the callback + + // Mandatory empty constructor public MasterListFragment() { } @@ -52,6 +59,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, // Set the adapter on the GridView gridView.setAdapter(mAdapter); + // TODO (3) Set a click listener on the gridView and trigger the callback onImageSelected when an item is clicked + // Return the root view return rootView; } From 0f437c04b0c267b0893dcc037e7a62ebbe53adc5 Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 23:31:21 -0800 Subject: [PATCH 9/9] TFragments.05-Solution-InterfaceCommunication --- .../android/android_me/ui/MainActivity.java | 11 ++++-- .../android_me/ui/MasterListFragment.java | 36 ++++++++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/example/android/android_me/ui/MainActivity.java b/app/src/main/java/com/example/android/android_me/ui/MainActivity.java index 0a1aa44bb..d8442038b 100755 --- a/app/src/main/java/com/example/android/android_me/ui/MainActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/MainActivity.java @@ -18,12 +18,13 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.widget.Toast; import com.example.android.android_me.R; // This activity is responsible for displaying the master list of all images -// TODO (4) Implement the MasterListFragment callback, OnImageClickListener -public class MainActivity extends AppCompatActivity { +// Implement the MasterListFragment callback, OnImageClickListener +public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener{ @Override @@ -33,6 +34,10 @@ protected void onCreate(Bundle savedInstanceState) { } - // TODO (5) Define the behavior for onImageSelected; create a Toast that displays the position clicked + // Define the behavior for onImageSelected + public void onImageSelected(int position) { + // Create a Toast that displays the position that was clicked + Toast.makeText(this, "Position clicked = " + position, Toast.LENGTH_SHORT).show(); + } } diff --git a/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java b/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java index 0c7e2404c..82095b898 100755 --- a/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/MasterListFragment.java @@ -16,11 +16,13 @@ package com.example.android.android_me.ui; +import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.GridView; import com.example.android.android_me.R; @@ -31,11 +33,28 @@ // The list appears as a grid of images public class MasterListFragment extends Fragment { - // TODO (1) Define a new interface OnImageClickListener that triggers a callback in the host activity - // The callback is a method named onImageSelected(int position) that contains information about - // which position on the grid of images a user has clicked + // Define a new interface OnImageClickListener that triggers a callback in the host activity + OnImageClickListener mCallback; - // TODO (2) Override onAttach to make sure that the container activity has implemented the callback + // OnImageClickListener interface, calls a method in the host activity named onImageSelected + public interface OnImageClickListener { + void onImageSelected(int position); + } + + // Override onAttach to make sure that the container activity has implemented the callback + @Override + public void onAttach(Context context) { + super.onAttach(context); + + // This makes sure that the host activity has implemented the callback interface + // If not, it throws an exception + try { + mCallback = (OnImageClickListener) context; + } catch (ClassCastException e) { + throw new ClassCastException(context.toString() + + " must implement OnImageClickListener"); + } + } // Mandatory empty constructor @@ -59,7 +78,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, // Set the adapter on the GridView gridView.setAdapter(mAdapter); - // TODO (3) Set a click listener on the gridView and trigger the callback onImageSelected when an item is clicked + // Set a click listener on the gridView and trigger the callback onImageSelected when an item is clicked + gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int position, long l) { + // Trigger the callback method and pass in the position that was clicked + mCallback.onImageSelected(position); + } + }); // Return the root view return rootView;