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 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.util.Log; import android.widget.FrameLayout; import android.widget.LinearLayout; import course.examples.Fragments.DynamicLayout.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 final QuotesFragment mQuoteFragment = new QuotesFragment(); private FragmentManager mFragmentManager; private FrameLayout mTitleFrameLayout, mQuotesFrameLayout; private static final int MATCH_PARENT = LinearLayout.LayoutParams.MATCH_PARENT; private static final String TAG = "QuoteViewerActivity"; @Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()"); 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 references to the TitleFragment and to the QuotesFragment mTitleFrameLayout = (FrameLayout) findViewById(R.id.title_fragment_container); mQuotesFrameLayout = (FrameLayout) findViewById(R.id.quote_fragment_container); // Get a reference to the FragmentManager mFragmentManager = getFragmentManager(); // Start a new FragmentTransaction FragmentTransaction fragmentTransaction = mFragmentManager .beginTransaction(); // Add the TitleFragment to the layout fragmentTransaction.add(R.id.title_fragment_container, new TitlesFragment()); // Commit the FragmentTransaction fragmentTransaction.commit(); // Add a OnBackStackChangedListener to reset the layout when the back stack changes mFragmentManager .addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { setLayout(); } }); } private void setLayout() { // Determine whether the QuoteFragment has been added if (!mQuoteFragment.isAdded()) { // Make the TitleFragment occupy the entire layout mTitleFrameLayout.setLayoutParams(new LinearLayout.LayoutParams( MATCH_PARENT, MATCH_PARENT)); mQuotesFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT)); } else { // Make the TitleLayout take 1/3 of the layout's width mTitleFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, 1f)); // Make the QuoteLayout take 2/3's of the layout's width mQuotesFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0, MATCH_PARENT, 2f)); } } // Called when the user selects an item in the TitlesFragment @Override public void onListSelection(int index) { // If the QuoteFragment has not been added, add it now if (!mQuoteFragment.isAdded()) { // Start a new FragmentTransaction FragmentTransaction fragmentTransaction = mFragmentManager .beginTransaction(); // Add the QuoteFragment to the layout fragmentTransaction.add(R.id.quote_fragment_container, mQuoteFragment); // Add this FragmentTransaction to the backstack fragmentTransaction.addToBackStack(null); // Commit the FragmentTransaction fragmentTransaction.commit(); // Force Android to execute the committed FragmentTransaction mFragmentManager.executePendingTransactions(); } if (mQuoteFragment.getShownIndex() != index) { // Tell the QuoteFragment to show the quote string at position index mQuoteFragment.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 106 107 108 109 110 |
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 static final String TAG = "QuotesFragment"; private TextView mQuoteView = null; private int mCurrIdx = -1; private int mQuoteArrLen; int getShownIndex() { return mCurrIdx; } // Show the Quote string at position newIndex void showQuoteAtIndex(int newIndex) { if (newIndex < 0 || newIndex >= mQuoteArrLen) 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) { Log.i(TAG, getClass().getSimpleName() + ":entered onCreateView()"); // 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) { Log.i(TAG, getClass().getSimpleName() + ":entered onActivityCreated()"); super.onActivityCreated(savedInstanceState); mQuoteView = (TextView) getActivity().findViewById(R.id.quoteView); mQuoteArrLen = 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 116 117 118 119 120 |
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 static final String TAG = "TitlesFragment"; private ListSelectionListener mListener = null; // 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) { Log.i(TAG, getClass().getSimpleName() + ":entered onAttach()"); 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) { Log.i(TAG, getClass().getSimpleName() + ":entered onActivityCreated()"); super.onActivityCreated(savedState); // 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)); // Set the list choice mode to allow only one selection at a time getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); } @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 |
<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" > <FrameLayout android:id="@+id/title_fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <FrameLayout android:id="@+id/quote_fragment_container" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout> |
quote_fragment.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/quoteView" android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="5dip" android:textSize="32sp" > </TextView> |
title_item.xml
1 2 3 4 5 6 7 8 9 |
<?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:padding="5dip" android:textSize="32sp" > </TextView> |