| 1 |
package ${packageName}; |
| 2 |
|
| 3 |
<#if includeCallbacks>import android.content.Context;</#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 applicationPackage??> |
| 11 |
import ${applicationPackage}.R; |
| 12 |
</#if> |
| 13 |
import com.google.android.gms.plus.PlusOneButton; |
| 14 |
|
| 15 |
/** |
| 16 |
* A fragment with a Google +1 button. |
| 17 |
<#if includeCallbacks> |
| 18 |
* Activities that contain this fragment must implement the |
| 19 |
* {@link ${className}.OnFragmentInteractionListener} interface |
| 20 |
* to handle interaction events. |
| 21 |
</#if> |
| 22 |
<#if includeFactory> |
| 23 |
* Use the {@link ${className}#newInstance} factory method to |
| 24 |
* create an instance of this fragment. |
| 25 |
</#if> |
| 26 |
* |
| 27 |
*/ |
| 28 |
public class ${className} extends Fragment { |
| 29 |
<#if includeFactory> |
| 30 |
// TODO: Rename parameter arguments, choose names that match |
| 31 |
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER |
| 32 |
private static final String ARG_PARAM1 = "param1"; |
| 33 |
private static final String ARG_PARAM2 = "param2"; |
| 34 |
|
| 35 |
// TODO: Rename and change types of parameters |
| 36 |
private String mParam1; |
| 37 |
private String mParam2; |
| 38 |
</#if> |
| 39 |
|
| 40 |
// The URL to +1. Must be a valid URL. |
| 41 |
private final String PLUS_ONE_URL = "http://developer.android.com"; |
| 42 |
|
| 43 |
// The request code must be 0 or greater. |
| 44 |
private static final int PLUS_ONE_REQUEST_CODE = 0; |
| 45 |
|
| 46 |
private PlusOneButton mPlusOneButton; |
| 47 |
|
| 48 |
<#if includeCallbacks> |
| 49 |
private OnFragmentInteractionListener mListener; |
| 50 |
</#if> |
| 51 |
|
| 52 |
<#if includeFactory> |
| 53 |
/** |
| 54 |
* Use this factory method to create a new instance of |
| 55 |
* this fragment using the provided parameters. |
| 56 |
* |
| 57 |
* @param param1 Parameter 1. |
| 58 |
* @param param2 Parameter 2. |
| 59 |
* @return A new instance of fragment ${className}. |
| 60 |
*/ |
| 61 |
// TODO: Rename and change types and number of parameters |
| 62 |
public static ${className} newInstance(String param1, String param2) { |
| 63 |
${className} fragment = new ${className}(); |
| 64 |
Bundle args = new Bundle(); |
| 65 |
args.putString(ARG_PARAM1, param1); |
| 66 |
args.putString(ARG_PARAM2, param2); |
| 67 |
fragment.setArguments(args); |
| 68 |
return fragment; |
| 69 |
} |
| 70 |
</#if> |
| 71 |
public ${className}() { |
| 72 |
// Required empty public constructor |
| 73 |
} |
| 74 |
|
| 75 |
<#if includeFactory> |
| 76 |
@Override |
| 77 |
public void onCreate(Bundle savedInstanceState) { |
| 78 |
super.onCreate(savedInstanceState); |
| 79 |
if (getArguments() != null) { |
| 80 |
mParam1 = getArguments().getString(ARG_PARAM1); |
| 81 |
mParam2 = getArguments().getString(ARG_PARAM2); |
| 82 |
} |
| 83 |
} |
| 84 |
</#if> |
| 85 |
|
| 86 |
@Override |
| 87 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 88 |
Bundle savedInstanceState) { |
| 89 |
// Inflate the layout for this fragment |
| 90 |
View view = inflater.inflate(R.layout.fragment_${classToResource(className)}, container, false); |
| 91 |
|
| 92 |
//Find the +1 button |
| 93 |
mPlusOneButton = (PlusOneButton) view.findViewById(R.id.plus_one_button); |
| 94 |
|
| 95 |
return view; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void onResume() { |
| 100 |
super.onResume(); |
| 101 |
|
| 102 |
// Refresh the state of the +1 button each time the activity receives focus. |
| 103 |
mPlusOneButton.initialize(PLUS_ONE_URL, PLUS_ONE_REQUEST_CODE); |
| 104 |
} |
| 105 |
|
| 106 |
<#if includeCallbacks> |
| 107 |
// TODO: Rename method, update argument and hook method into UI event |
| 108 |
public void onButtonPressed(Uri uri) { |
| 109 |
if (mListener != null) { |
| 110 |
mListener.onFragmentInteraction(uri); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public void onAttach(Context context) { |
| 116 |
super.onAttach(context); |
| 117 |
if (context instanceof OnFragmentInteractionListener) { |
| 118 |
mListener = (OnFragmentInteractionListener) context; |
| 119 |
} else { |
| 120 |
throw new RuntimeException(context.toString() |
| 121 |
+ " must implement OnFragmentInteractionListener"); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void onDetach() { |
| 127 |
super.onDetach(); |
| 128 |
mListener = null; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* This interface must be implemented by activities that contain this |
| 133 |
* fragment to allow an interaction in this fragment to be communicated |
| 134 |
* to the activity and potentially other fragments contained in that |
| 135 |
* activity. |
| 136 |
* <p> |
| 137 |
* See the Android Training lesson <a href= |
| 138 |
* "http://developer.android.com/training/basics/fragments/communicating.html" |
| 139 |
* >Communicating with Other Fragments</a> for more information. |
| 140 |
*/ |
| 141 |
public interface OnFragmentInteractionListener { |
| 142 |
// TODO: Update argument type and name |
| 143 |
void onFragmentInteraction(Uri uri); |
| 144 |
} |
| 145 |
</#if> |
| 146 |
|
| 147 |
} |