QuoteViewerActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import android.app.Activity; import android.os.Bundle; import android.util.Log; import course.examples.Fragments.StaticLayout.TitlesFragment.ListSelectionListener; //Several Activity lifecycle methods are instrumented to emit LogCat output //so you can follow this class' lifecycle public class QuoteViewerActivity extends Activity implements ListSelectionListener { public static String[] mTitleArray; public static String[] mQuoteArray; private QuotesFragment mDetailsFragment; private static final String TAG = "QuoteViewerActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the string arrays with the titles and qutoes mTitleArray = getResources().getStringArray(R.array.Titles); mQuoteArray = getResources().getStringArray(R.array.Quotes); setContentView(R.layout.main); // Get a reference to the QuotesFragment mDetailsFragment = (QuotesFragment) getFragmentManager() .findFragmentById(R.id.details); } // Called when the user selects an item in the TitlesFragment @Override public void onListSelection(int index) { if (mDetailsFragment.getShownIndex() != index) { // Tell the QuoteFragment to show the quote string at position index mDetailsFragment.showQuoteAtIndex(index); } } @Override protected void onDestroy() { Log.i(TAG, getClass().getSimpleName() + ":entered onDestroy()"); super.onDestroy(); } @Override protected void onPause() { Log.i(TAG, getClass().getSimpleName() + ":entered onPause()"); super.onPause(); } @Override protected void onRestart() { Log.i(TAG, getClass().getSimpleName() + ":entered onRestart()"); super.onRestart(); } @Override protected void onResume() { Log.i(TAG, getClass().getSimpleName() + ":entered onResume()"); super.onResume(); } @Override protected void onStart() { Log.i(TAG, getClass().getSimpleName() + ":entered onStart()"); super.onStart(); } @Override protected void onStop() { Log.i(TAG, getClass().getSimpleName() + ":entered onStop()"); super.onStop(); } } |
QuotesFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; //Several Activity and Fragment lifecycle methods are instrumented to emit LogCat output //so you can follow the class' lifecycle public class QuotesFragment extends Fragment { private TextView mQuoteView = null; private int mCurrIdx = -1; private int mQuoteArrayLen; private static final String TAG = "QuotesFragment"; public int getShownIndex() { return mCurrIdx; } // Show the Quote string at position newIndex public void showQuoteAtIndex(int newIndex) { if (newIndex < 0 || newIndex >= mQuoteArrayLen) return; mCurrIdx = newIndex; mQuoteView.setText(QuoteViewerActivity.mQuoteArray[mCurrIdx]); } @Override public void onAttach(Activity activity) { Log.i(TAG, getClass().getSimpleName() + ":entered onAttach()"); super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()"); super.onCreate(savedInstanceState); } // Called to create the content view for this Fragment @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout defined in quote_fragment.xml // The last parameter is false because the returned view does not need to be attached to the container ViewGroup return inflater.inflate(R.layout.quote_fragment, container, false); } // Set up some information about the mQuoteView TextView @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mQuoteView = (TextView) getActivity().findViewById(R.id.quoteView); mQuoteArrayLen = QuoteViewerActivity.mQuoteArray.length; } @Override public void onStart() { Log.i(TAG, getClass().getSimpleName() + ":entered onStart()"); super.onStart(); } @Override public void onResume() { Log.i(TAG, getClass().getSimpleName() + ":entered onResume()"); super.onResume(); } @Override public void onPause() { Log.i(TAG, getClass().getSimpleName() + ":entered onPause()"); super.onPause(); } @Override public void onStop() { Log.i(TAG, getClass().getSimpleName() + ":entered onStop()"); super.onStop(); } @Override public void onDetach() { Log.i(TAG, getClass().getSimpleName() + ":entered onDetach()"); super.onDetach(); } @Override public void onDestroy() { Log.i(TAG, getClass().getSimpleName() + ":entered onDestroy()"); super.onDestroy(); } @Override public void onDestroyView() { Log.i(TAG, getClass().getSimpleName() + ":entered onDestroyView()"); super.onDestroyView(); } } |
TitlesFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import android.app.Activity; import android.app.ListFragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; //Several Activity and Fragment lifecycle methods are instrumented to emit LogCat output //so you can follow the class' lifecycle public class TitlesFragment extends ListFragment { private ListSelectionListener mListener = null; private static final String TAG = "TitlesFragment"; // Callback interface that allows this Fragment to notify the QuoteViewerActivity when // user clicks on a List Item public interface ListSelectionListener { public void onListSelection(int index); } // Called when the user selects an item from the List @Override public void onListItemClick(ListView l, View v, int pos, long id) { // Indicates the selected item has been checked getListView().setItemChecked(pos, true); // Inform the QuoteViewerActivity that the item in position pos has been selected mListener.onListSelection(pos); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { // Set the ListSelectionListener for communicating with the QuoteViewerActivity mListener = (ListSelectionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); } } @Override public void onCreate(Bundle savedInstanceState) { Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()"); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()"); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onActivityCreated(Bundle savedState) { super.onActivityCreated(savedState); // Set the list choice mode to allow only one selection at a time getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // Set the list adapter for the ListView // Discussed in more detail in the user interface classes lesson setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.title_item, QuoteViewerActivity.mTitleArray)); } @Override public void onStart() { Log.i(TAG, getClass().getSimpleName() + ":entered onStart()"); super.onStart(); } @Override public void onResume() { Log.i(TAG, getClass().getSimpleName() + ":entered onResume()"); super.onResume(); } @Override public void onPause() { Log.i(TAG, getClass().getSimpleName() + ":entered onPause()"); super.onPause(); } @Override public void onStop() { Log.i(TAG, getClass().getSimpleName() + ":entered onStop()"); super.onStop(); } @Override public void onDetach() { Log.i(TAG, getClass().getSimpleName() + ":entered onDetach()"); super.onDetach(); } @Override public void onDestroy() { Log.i(TAG, getClass().getSimpleName() + ":entered onDestroy()"); super.onDestroy(); } @Override public void onDestroyView() { Log.i(TAG, getClass().getSimpleName() + ":entered onDestroyView()"); super.onDestroyView(); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <fragment android:id="@+id/titles" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" class="course.examples.Fragments.StaticLayout.TitlesFragment" /> <fragment android:id="@+id/details" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="2" class="course.examples.Fragments.StaticLayout.QuotesFragment" /> </LinearLayout> |
list_item.xml
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?android:attr/activatedBackgroundIndicator" android:orientation="vertical" android:padding="5dip" android:textSize="32sp" > </TextView> |
quote_fragment.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/quoteView" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dip" android:textSize="32sp" > </TextView> </LinearLayout> |