Created
September 26, 2017 23:12
-
-
Save stephanenicolas/09d19130d6a4b9e572c533f18d6ebd50 to your computer and use it in GitHub Desktop.
Good Old Way: Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HeadlinesFragment extends ListFragment { | |
HeadlineListener mCallback; | |
// Container Activity must implement this interface | |
public interface HeadlineListener { | |
public void onArticleSelected(int position); | |
} | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
// Makes sure that the container activity has implemented | |
// the callback interface. If not, it throws an exception | |
try { | |
mCallback = (HeadlineListener) activity; | |
} catch (ClassCastException e) { | |
throw new ClassCastException(activity.toString() | |
+ " must implement HeadlineListener"); | |
} | |
} | |
@Override | |
public void onListItemClick(ListView l, View v, int p, long i) { | |
// Send the event to the host activity | |
mCallback.onArticleSelected(p); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public class HeadlinesFragment extends ListFragment {
There is a typo --> `HeadlineFragmentHeadlineListener mCallback;
Hungarian notation?