Guest User

HackFest YorkU App (ItemListFragment)

a guest
Feb 26th, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. package com.example.yulabs;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.support.v4.app.ListFragment;
  6. import android.view.View;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ListView;
  9.  
  10. import com.example.yulabs.dummy.DummyContent;
  11.  
  12. /**
  13.  * A list fragment representing a list of Items. This fragment
  14.  * also supports tablet devices by allowing list items to be given an
  15.  * 'activated' state upon selection. This helps indicate which item is
  16.  * currently being viewed in a {@link ItemDetailFragment}.
  17.  * <p>
  18.  * Activities containing this fragment MUST implement the {@link Callbacks}
  19.  * interface.
  20.  */
  21. public class ItemListFragment extends ListFragment {
  22.  
  23.     /**
  24.      * The serialization (saved instance state) Bundle key representing the
  25.      * activated item position. Only used on tablets.
  26.      */
  27.     private static final String STATE_ACTIVATED_POSITION = "activated_position";
  28.  
  29.     /**
  30.      * The fragment's current callback object, which is notified of list item
  31.      * clicks.
  32.      */
  33.     private Callbacks mCallbacks = sDummyCallbacks;
  34.  
  35.     /**
  36.      * The current activated item position. Only used on tablets.
  37.      */
  38.     private int mActivatedPosition = ListView.INVALID_POSITION;
  39.  
  40.     /**
  41.      * A callback interface that all activities containing this fragment must
  42.      * implement. This mechanism allows activities to be notified of item
  43.      * selections.
  44.      */
  45.     public interface Callbacks {
  46.         /**
  47.          * Callback for when an item has been selected.
  48.          */
  49.         public void onItemSelected(String id);
  50.     }
  51.  
  52.     /**
  53.      * A dummy implementation of the {@link Callbacks} interface that does
  54.      * nothing. Used only when this fragment is not attached to an activity.
  55.      */
  56.     private static Callbacks sDummyCallbacks = new Callbacks() {
  57.         @Override
  58.         public void onItemSelected(String id) {
  59.         }
  60.     };
  61.  
  62.     /**
  63.      * Mandatory empty constructor for the fragment manager to instantiate the
  64.      * fragment (e.g. upon screen orientation changes).
  65.      */
  66.     public ItemListFragment() {
  67.     }
  68.  
  69.     @Override
  70.     public void onCreate(Bundle savedInstanceState) {
  71.         super.onCreate(savedInstanceState);
  72.  
  73.         // TODO: replace with a real list adapter.
  74.         setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
  75.                 getActivity(),
  76.                 android.R.layout.simple_list_item_activated_1,
  77.                 android.R.id.text1,
  78.                 DummyContent.ITEMS));
  79.     }
  80.  
  81.     @Override
  82.     public void onViewCreated(View view, Bundle savedInstanceState) {
  83.         super.onViewCreated(view, savedInstanceState);
  84.  
  85.         // Restore the previously serialized activated item position.
  86.         if (savedInstanceState != null
  87.                 && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
  88.             setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
  89.         }
  90.     }
  91.  
  92.     @Override
  93.     public void onAttach(Activity activity) {
  94.         super.onAttach(activity);
  95.  
  96.         // Activities containing this fragment must implement its callbacks.
  97.         if (!(activity instanceof Callbacks)) {
  98.             throw new IllegalStateException("Activity must implement fragment's callbacks.");
  99.         }
  100.  
  101.         mCallbacks = (Callbacks) activity;
  102.     }
  103.  
  104.     @Override
  105.     public void onDetach() {
  106.         super.onDetach();
  107.  
  108.         // Reset the active callbacks interface to the dummy implementation.
  109.         mCallbacks = sDummyCallbacks;
  110.     }
  111.  
  112.     @Override
  113.     public void onListItemClick(ListView listView, View view, int position, long id) {
  114.         super.onListItemClick(listView, view, position, id);
  115.  
  116.         // Notify the active callbacks interface (the activity, if the
  117.         // fragment is attached to one) that an item has been selected.
  118.         mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
  119.     }
  120.  
  121.     @Override
  122.     public void onSaveInstanceState(Bundle outState) {
  123.         super.onSaveInstanceState(outState);
  124.         if (mActivatedPosition != ListView.INVALID_POSITION) {
  125.             // Serialize and persist the activated item position.
  126.             outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
  127.         }
  128.     }
  129.  
  130.     /**
  131.      * Turns on activate-on-click mode. When this mode is on, list items will be
  132.      * given the 'activated' state when touched.
  133.      */
  134.     public void setActivateOnItemClick(boolean activateOnItemClick) {
  135.         // When setting CHOICE_MODE_SINGLE, ListView will automatically
  136.         // give items the 'activated' state when touched.
  137.         getListView().setChoiceMode(activateOnItemClick
  138.                 ? ListView.CHOICE_MODE_SINGLE
  139.                 : ListView.CHOICE_MODE_NONE);
  140.     }
  141.  
  142.     private void setActivatedPosition(int position) {
  143.         if (position == ListView.INVALID_POSITION) {
  144.             getListView().setItemChecked(mActivatedPosition, false);
  145.         } else {
  146.             getListView().setItemChecked(position, true);
  147.         }
  148.  
  149.         mActivatedPosition = position;
  150.     }
  151. }
Add Comment
Please, Sign In to add comment