From 1299057ca99a104c2782b4621515cb4aad99f8c4 Mon Sep 17 00:00:00 2001 From: Zikstar Date: Tue, 18 Jun 2019 15:18:17 +0100 Subject: [PATCH] exercise complete --- .idea/caches/build_file_checksums.ser | Bin 0 -> 496 bytes .idea/encodings.xml | 4 +++ .idea/gradle.xml | 18 ++++++++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 9 +++++ .idea/runConfigurations.xml | 12 +++++++ .idea/vcs.xml | 6 ++++ .../android/android_me/ui/MainActivity.java | 9 ++++- .../android_me/ui/MasterListFragment.java | 33 ++++++++++++++++++ 9 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 .idea/caches/build_file_checksums.ser create mode 100644 .idea/encodings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser new file mode 100644 index 0000000000000000000000000000000000000000..0450824c14fffd02b0e26a75c292b14ea28d1b89 GIT binary patch literal 496 zcmZ4UmVvdnh`~NNKUXg?FQq6yGexf?KR>5fFEb@IQ7^qHF(oHeub?PDD>b=9F91S2 zm1gFoxMk*~I%lLNXBU^|7Q2L-Ts|(GuF1r}iGcy+BnE*i zvOPEZ88)%JY~Y;n=7&>V34=glK|xFsC@`RAl@>D8F|dIY=Nc?so49(>^she-_ZeSi z@dx^|IJKlCGcUauL+!kq3HxmBv*+n1=t+e;9bO1DI43_jF$WavFekwc)?eQ0`5@iJ mYwx8svfbK~1WFhLF+BiRy-(oJffE0WTJNnHorR5yf+_%y_pJ8- literal 0 HcmV?d00001 diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 000000000..15a15b218 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 000000000..7ac24c777 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..7631aec35 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..fd6b9abf3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 000000000..7f68460d8 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..94a25f7f4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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..39ae76e5b 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 { +public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener { @Override @@ -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 } 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..eb2c82d5d 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; @@ -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, @@ -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; }