Home | History | Annotate | Download | only in app_package
      1 package ${packageName};
      2 
      3 <#if includeCallbacks>import android.app.Activity;</#if>
      4 <#if includeCallbacks>import android.net.Uri;</#if>
      5 import android.os.Bundle;
      6 import android.support.v4.app.Fragment;
      7 import android.view.LayoutInflater;
      8 import android.view.View;
      9 import android.view.ViewGroup;
     10 <#if !includeLayout>import android.widget.TextView;</#if>
     11 
     12 /**
     13  * A simple {@link android.support.v4.app.Fragment} subclass.
     14 <#if includeCallbacks>
     15  * Activities that contain this fragment must implement the
     16  * {@link ${className}.OnFragmentInteractionListener} interface
     17  * to handle interaction events.
     18 </#if>
     19 <#if includeFactory>
     20  * Use the {@link ${className}#newInstance} factory method to
     21  * create an instance of this fragment.
     22 </#if>
     23  *
     24  */
     25 public class ${className} extends Fragment {
     26 <#if includeFactory>
     27     // TODO: Rename parameter arguments, choose names that match
     28     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
     29     private static final String ARG_PARAM1 = "param1";
     30     private static final String ARG_PARAM2 = "param2";
     31 
     32     // TODO: Rename and change types of parameters
     33     private String mParam1;
     34     private String mParam2;
     35 </#if>
     36 
     37 <#if includeCallbacks>
     38     private OnFragmentInteractionListener mListener;
     39 </#if>
     40 
     41 <#if includeFactory>
     42     /**
     43      * Use this factory method to create a new instance of
     44      * this fragment using the provided parameters.
     45      *
     46      * @param param1 Parameter 1.
     47      * @param param2 Parameter 2.
     48      * @return A new instance of fragment ${className}.
     49      */
     50     // TODO: Rename and change types and number of parameters
     51     public static ${className} newInstance(String param1, String param2) {
     52         ${className} fragment = new ${className}();
     53         Bundle args = new Bundle();
     54         args.putString(ARG_PARAM1, param1);
     55         args.putString(ARG_PARAM2, param2);
     56         fragment.setArguments(args);
     57         return fragment;
     58     }
     59 </#if>
     60     public ${className}() {
     61         // Required empty public constructor
     62     }
     63 
     64 <#if includeFactory>
     65     @Override
     66     public void onCreate(Bundle savedInstanceState) {
     67         super.onCreate(savedInstanceState);
     68         if (getArguments() != null) {
     69             mParam1 = getArguments().getString(ARG_PARAM1);
     70             mParam2 = getArguments().getString(ARG_PARAM2);
     71         }
     72     }
     73 </#if>
     74 
     75     @Override
     76     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     77                              Bundle savedInstanceState) {
     78 <#if includeLayout>
     79         // Inflate the layout for this fragment
     80         return inflater.inflate(R.layout.fragment_${classToResource(className)}, container, false);
     81 <#else>
     82         TextView textView = new TextView(getActivity());
     83         textView.setText(R.string.hello_blank_fragment);
     84         return textView;
     85 </#if>
     86     }
     87 
     88 <#if includeCallbacks>
     89     // TODO: Rename method, update argument and hook method into UI event
     90     public void onButtonPressed(Uri uri) {
     91         if (mListener != null) {
     92             mListener.onFragmentInteraction(uri);
     93         }
     94     }
     95 
     96     @Override
     97     public void onAttach(Activity activity) {
     98         super.onAttach(activity);
     99         try {
    100             mListener = (OnFragmentInteractionListener) activity;
    101         } catch (ClassCastException e) {
    102             throw new ClassCastException(activity.toString()
    103                     + " must implement OnFragmentInteractionListener");
    104         }
    105     }
    106 
    107     @Override
    108     public void onDetach() {
    109         super.onDetach();
    110         mListener = null;
    111     }
    112 
    113     /**
    114      * This interface must be implemented by activities that contain this
    115      * fragment to allow an interaction in this fragment to be communicated
    116      * to the activity and potentially other fragments contained in that
    117      * activity.
    118      * <p>
    119      * See the Android Training lesson <a href=
    120      * "http://developer.android.com/training/basics/fragments/communicating.html"
    121      * >Communicating with Other Fragments</a> for more information.
    122      */
    123     public interface OnFragmentInteractionListener {
    124         // TODO: Update argument type and name
    125         public void onFragmentInteraction(Uri uri);
    126     }
    127 </#if>
    128 
    129 }
    130