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

T fragments.05 solution interface communication #30

Open
wants to merge 9 commits into
base: TFragments.01-Exercise-CreateBodyPartFragment
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
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.AndroidMeActivity">

<!-- Declare the MainActivity in the manifest and set it to launch upon opening this app -->
<activity android:name=".ui.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ui.AndroidMeActivity" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,53 @@

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 {

// 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
// Only create new fragments when there is no previously saved state
if(savedInstanceState == null) {

// 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,120 @@
/*
* 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.ArrayList;
import java.util.List;

public class BodyPartFragment extends Fragment {

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

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

// 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
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
if(mImageIds != null){
// Set the image resource to the list item at the stored index
imageView.setImageResource(mImageIds.get(mListIndex));

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

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

/**
* Save the current state of this fragment
*/
@Override
public void onSaveInstanceState(Bundle currentState) {
currentState.putIntegerArrayList(IMAGE_ID_LIST, (ArrayList<Integer>) mImageIds);
currentState.putInt(LIST_INDEX, mListIndex);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 android.widget.Toast;

import com.example.android.android_me.R;

// This activity is responsible for displaying the master list of all images
// Implement the MasterListFragment callback, OnImageClickListener
public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener{


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

}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.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;
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 {

// Define a new interface OnImageClickListener that triggers a callback in the host activity
OnImageClickListener mCallback;

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

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

}
19 changes: 17 additions & 2 deletions app/src/main/res/layout/activity_android_me.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,24 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<!-- TODO (4) Create a container to hold the head BodyPartFragment of the custom Android -->
<!--The container should be 180dp in height -->

<!-- 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
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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.-->

<!-- Display the static master list fragment -->
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/master_list_fragment"
android:name="com.example.android.android_me.ui.MasterListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Loading