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

exercise complete #61

Open
wants to merge 1 commit into
base: TFragments.05-Exercise-InterfaceCommunication
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
Binary file added .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener {


@Override
Expand All @@ -33,6 +34,12 @@ protected void onCreate(Bundle savedInstanceState) {

}

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

// TODO (5) Define the behavior for onImageSelected; create a Toast that displays the position clicked

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,13 +37,36 @@ public class MasterListFragment extends Fragment {
// The callback is a method named onImageSelected(int position) that contains information about
// which position on the grid of images a user has clicked


OnImageClickListener mCallback;

public interface OnImageClickListener{
void onImageSelected(int position);
}

// TODO (2) Override onAttach to make sure that the container activity has implemented the callback

@Override
public void onAttach(Context context) {
super.onAttach(context);

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,
Expand All @@ -61,6 +86,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

// TODO (3) 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 tbe callback method and pass in the position that was clicked
mCallback.onImageSelected(position);
}
});

// Return the root view
return rootView;
}
Expand Down