Roll NDK to pick std::deque patch.
[android_tools.git] / sdk / tools / templates / other / ListFragment / root / src / app_package / ListFragment.java.ftl
blob2e130a5640ea891243fbb4eefd0c02e05b04d6c3
1 package ${packageName};
3 import android.app.Activity;
4 import android.os.Bundle;
5 <#if switchGrid == true>
6 import android${SupportPackage}.app.Fragment;
7 import android.view.LayoutInflater;
8 <#else>
9 import android${SupportPackage}.app.ListFragment;
10 </#if>
11 import android.view.View;
12 <#if switchGrid == true>
13 import android.view.ViewGroup;
14 import android.widget.AbsListView;
15 import android.widget.AdapterView;
16 </#if>
17 import android.widget.ArrayAdapter;
18 <#if switchGrid == true>
19 import android.widget.ListAdapter;
20 import android.widget.TextView;
21 <#else>
22 import android.widget.ListView;
23 </#if>
24 <#if applicationPackage??>import ${applicationPackage}.R;</#if>
26 import ${packageName}.dummy.DummyContent;
28 /**
29  * A fragment representing a list of Items.
30  * <p />
31 <#if switchGrid == true>
32  * Large screen devices (such as tablets) are supported by replacing the ListView
33  * with a GridView.
34 </#if>
35  * <p />
36  * Activities containing this fragment MUST implement the {@link Callbacks}
37  * interface.
38  */
39 <#if switchGrid == true>
40 public class ${className} extends Fragment implements AbsListView.OnItemClickListener {
41 <#else>
42 public class ${className} extends ListFragment {
43 </#if>
45 <#if includeFactory>
46     // TODO: Rename parameter arguments, choose names that match
47     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
48     private static final String ARG_PARAM1 = "param1";
49     private static final String ARG_PARAM2 = "param2";
51     // TODO: Rename and change types of parameters
52     private String mParam1;
53     private String mParam2;
55 </#if>
56     private OnFragmentInteractionListener mListener;
58 <#if switchGrid == true>
59     /**
60      * The fragment's ListView/GridView.
61      */
62     private AbsListView mListView;
64     /**
65      * The Adapter which will be used to populate the ListView/GridView with
66      * Views.
67      */
68     private ListAdapter mAdapter;
70 </#if>
71 <#if includeFactory>
72     // TODO: Rename and change types of parameters
73     public static ${className} newInstance(String param1, String param2) {
74         ${className} fragment = new ${className}();
75         Bundle args = new Bundle();
76         args.putString(ARG_PARAM1, param1);
77         args.putString(ARG_PARAM2, param2);
78         fragment.setArguments(args);
79         return fragment;
80     }
82 </#if>
83     /**
84      * Mandatory empty constructor for the fragment manager to instantiate the
85      * fragment (e.g. upon screen orientation changes).
86      */
87     public ${className}() {
88     }
90     @Override
91     public void onCreate(Bundle savedInstanceState) {
92         super.onCreate(savedInstanceState);
94 <#if includeFactory>
95         if (getArguments() != null) {
96             mParam1 = getArguments().getString(ARG_PARAM1);
97             mParam2 = getArguments().getString(ARG_PARAM2);
98         }
99 </#if>
101         // TODO: Change Adapter to display your content
102 <#if switchGrid == true>
103         mAdapter = new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
104                 android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS);
105 <#else>
106         setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
107                 android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS));
108 </#if>
109     }
111 <#if switchGrid == true>
112     @Override
113     public View onCreateView(LayoutInflater inflater, ViewGroup container,
114                              Bundle savedInstanceState) {
115         View view = inflater.inflate(R.layout.${fragment_layout}, container, false);
117         // Set the adapter
118         mListView = (AbsListView) view.findViewById(android.R.id.list);
119         ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
121         // Set OnItemClickListener so we can be notified on item clicks
122         mListView.setOnItemClickListener(this);
124         return view;
125     }
126 </#if>
128     @Override
129     public void onAttach(Activity activity) {
130         super.onAttach(activity);
131         try {
132             mListener = (OnFragmentInteractionListener) activity;
133         } catch (ClassCastException e) {
134             throw new ClassCastException(activity.toString()
135                 + " must implement OnFragmentInteractionListener");
136         }
137     }
139     @Override
140     public void onDetach() {
141         super.onDetach();
142         mListener = null;
143     }
145 <#if switchGrid == true>
146     @Override
147     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
148         if (null != mListener) {
149             // Notify the active callbacks interface (the activity, if the
150             // fragment is attached to one) that an item has been selected.
151             mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
152         }
153     }
155     /**
156      * The default content for this Fragment has a TextView that is shown when
157      * the list is empty. If you would like to change the text, call this method
158      * to supply the text it should use.
159      */
160     public void setEmptyText(CharSequence emptyText) {
161         View emptyView = mListView.getEmptyView();
163         if (emptyText instanceof TextView) {
164             ((TextView) emptyView).setText(emptyText);
165         }
166     }
167 <#else>
168     @Override
169     public void onListItemClick(ListView l, View v, int position, long id) {
170         super.onListItemClick(l, v, position, id);
172         if (null != mListener) {
173             // Notify the active callbacks interface (the activity, if the
174             // fragment is attached to one) that an item has been selected.
175             mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
176         }
177     }
178 </#if>
180     /**
181     * This interface must be implemented by activities that contain this
182     * fragment to allow an interaction in this fragment to be communicated
183     * to the activity and potentially other fragments contained in that
184     * activity.
185     * <p>
186     * See the Android Training lesson <a href=
187     * "http://developer.android.com/training/basics/fragments/communicating.html"
188     * >Communicating with Other Fragments</a> for more information.
189     */
190     public interface OnFragmentInteractionListener {
191         // TODO: Update argument type and name
192         public void onFragmentInteraction(String id);
193     }