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