Home | History | Annotate | Download | only in app_package
      1 package ${packageName};
      2 
      3 import android.app.Activity;
      4 import android.os.Bundle;
      5 <#if switchGrid == true>
      6 import android.support.v4.app.Fragment;
      7 import android.view.LayoutInflater;
      8 <#else>
      9 import android.support.v4.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 
     25 import ${packageName}.dummy.DummyContent;
     26 
     27 /**
     28  * A fragment representing a list of Items.
     29  * <p />
     30 <#if switchGrid == true>
     31  * Large screen devices (such as tablets) are supported by replacing the ListView
     32  * with a GridView.
     33 </#if>
     34  * <p />
     35  * Activities containing this fragment MUST implement the {@link Callbacks}
     36  * interface.
     37  */
     38 <#if switchGrid == true>
     39 public class ${className} extends Fragment implements AbsListView.OnItemClickListener {
     40 <#else>
     41 public class ${className} extends ListFragment {
     42 </#if>
     43 
     44 <#if includeFactory>
     45     // TODO: Rename parameter arguments, choose names that match
     46     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
     47     private static final String ARG_PARAM1 = "param1";
     48     private static final String ARG_PARAM2 = "param2";
     49 
     50     // TODO: Rename and change types of parameters
     51     private String mParam1;
     52     private String mParam2;
     53 
     54 </#if>
     55     private OnFragmentInteractionListener mListener;
     56 
     57 <#if switchGrid == true>
     58     /**
     59      * The fragment's ListView/GridView.
     60      */
     61     private AbsListView mListView;
     62 
     63     /**
     64      * The Adapter which will be used to populate the ListView/GridView with
     65      * Views.
     66      */
     67     private ListAdapter mAdapter;
     68 
     69 </#if>
     70 <#if includeFactory>
     71     // TODO: Rename and change types of parameters
     72     public static ${className} newInstance(String param1, String param2) {
     73         ${className} fragment = new ${className}();
     74         Bundle args = new Bundle();
     75         args.putString(ARG_PARAM1, param1);
     76         args.putString(ARG_PARAM2, param2);
     77         fragment.setArguments(args);
     78         return fragment;
     79     }
     80 
     81 </#if>
     82     /**
     83      * Mandatory empty constructor for the fragment manager to instantiate the
     84      * fragment (e.g. upon screen orientation changes).
     85      */
     86     public ${className}() {
     87     }
     88 
     89     @Override
     90     public void onCreate(Bundle savedInstanceState) {
     91         super.onCreate(savedInstanceState);
     92 
     93 <#if includeFactory>
     94         if (getArguments() != null) {
     95             mParam1 = getArguments().getString(ARG_PARAM1);
     96             mParam2 = getArguments().getString(ARG_PARAM2);
     97         }
     98 </#if>
     99 
    100         // TODO: Change Adapter to display your content
    101 <#if switchGrid == true>
    102         mAdapter = new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
    103                 android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS);
    104 <#else>
    105         setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
    106                 android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS));
    107 </#if>
    108     }
    109 
    110 <#if switchGrid == true>
    111     @Override
    112     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    113                              Bundle savedInstanceState) {
    114         View view = inflater.inflate(R.layout.${fragment_layout}, container, false);
    115 
    116         // Set the adapter
    117         mListView = (AbsListView) view.findViewById(android.R.id.list);
    118         ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
    119 
    120         // Set OnItemClickListener so we can be notified on item clicks
    121         mListView.setOnItemClickListener(this);
    122 
    123         return view;
    124     }
    125 </#if>
    126 
    127     @Override
    128     public void onAttach(Activity activity) {
    129         super.onAttach(activity);
    130         try {
    131             mListener = (OnFragmentInteractionListener) activity;
    132         } catch (ClassCastException e) {
    133             throw new ClassCastException(activity.toString()
    134                 + " must implement OnFragmentInteractionListener");
    135         }
    136     }
    137 
    138     @Override
    139     public void onDetach() {
    140         super.onDetach();
    141         mListener = null;
    142     }
    143 
    144 
    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     }
    154 
    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();
    162 
    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);
    171 
    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>
    179 
    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     }
    194 
    195 }
    196