Debian Codegraph @

templates/other/ListFragment/root/src/app_package/ListFragment.java.ftl

raw
1 package ${packageName};
2
3 import android.content.Context;
4 import android.os.Bundle;
5 import android${SupportPackage}.app.Fragment;
6 import android.support.v7.widget.GridLayoutManager;
7 import android.support.v7.widget.LinearLayoutManager;
8 import android.support.v7.widget.RecyclerView;
9 import android.view.LayoutInflater;
10 import android.view.View;
11 import android.view.ViewGroup;
12 <#if applicationPackage??>
13 import ${applicationPackage}.R;
14 </#if>
15 import ${packageName}.dummy.DummyContent;
16 import ${packageName}.dummy.DummyContent.DummyItem;
17
18 import java.util.List;
19
20 /**
21 * A fragment representing a list of Items.
22 * <p />
23 * Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
24 * interface.
25 */
26 public class ${className} extends Fragment {
27
28 // TODO: Customize parameters
29 private int mColumnCount = ${columnCount};
30
31 <#if includeFactory>
32 // TODO: Customize parameter argument names
33 private static final String ARG_COLUMN_COUNT = "column-count";
34
35 </#if>
36 private OnListFragmentInteractionListener mListener;
37
38 <#if includeFactory>
39 // TODO: Customize parameter initialization
40 @SuppressWarnings("unused")
41 public static ${className} newInstance(int columnCount) {
42 ${className} fragment = new ${className}();
43 Bundle args = new Bundle();
44 args.putInt(ARG_COLUMN_COUNT, columnCount);
45 fragment.setArguments(args);
46 return fragment;
47 }
48
49 </#if>
50 /**
51 * Mandatory empty constructor for the fragment manager to instantiate the
52 * fragment (e.g. upon screen orientation changes).
53 */
54 public ${className}() {
55 }
56
57 @Override
58 public void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60
61 <#if includeFactory>
62 if (getArguments() != null) {
63 mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
64 }
65 </#if>
66 }
67
68 @Override
69 public View onCreateView(LayoutInflater inflater, ViewGroup container,
70 Bundle savedInstanceState) {
71 View view = inflater.inflate(R.layout.${fragment_layout_list}, container, false);
72
73 // Set the adapter
74 if (view instanceof RecyclerView) {
75 Context context = view.getContext();
76 RecyclerView recyclerView = (RecyclerView) view;
77 if (mColumnCount <= 1) {
78 recyclerView.setLayoutManager(new LinearLayoutManager(context));
79 } else {
80 recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
81 }
82 recyclerView.setAdapter(new ${adapterClassName}(DummyContent.ITEMS, mListener));
83 }
84 return view;
85 }
86
87
88 @Override
89 public void onAttach(Context context) {
90 super.onAttach(context);
91 if (context instanceof OnListFragmentInteractionListener) {
92 mListener = (OnListFragmentInteractionListener) context;
93 } else {
94 throw new RuntimeException(context.toString()
95 + " must implement OnListFragmentInteractionListener");
96 }
97 }
98
99 @Override
100 public void onDetach() {
101 super.onDetach();
102 mListener = null;
103 }
104
105 /**
106 * This interface must be implemented by activities that contain this
107 * fragment to allow an interaction in this fragment to be communicated
108 * to the activity and potentially other fragments contained in that
109 * activity.
110 * <p/>
111 * See the Android Training lesson <a href=
112 * "http://developer.android.com/training/basics/fragments/communicating.html"
113 * >Communicating with Other Fragments</a> for more information.
114 */
115 public interface OnListFragmentInteractionListener {
116 // TODO: Update argument type and name
117 void onListFragmentInteraction(DummyItem item);
118 }
119 }