7
7
import android .view .LayoutInflater ;
8
8
import android .view .View ;
9
9
import android .view .ViewGroup ;
10
+ import android .widget .Button ;
10
11
import android .widget .ImageView ;
11
12
import android .widget .TextView ;
12
13
@@ -27,17 +28,8 @@ public class OnboardingFragment extends Fragment implements Serializable {
27
28
private static final String TITLE = "TITLE" ;
28
29
private static final String BODY_TEXT = "BODY_TEXT" ;
29
30
private static final String IMAGE_RESOURCE_ID = "IMAGE_RESOURCE_ID" ;
30
-
31
- /** The title which is displayed at the top of the fragment */
32
- private String title ;
33
-
34
- /** The body text which is displayed in the middle of the fragment */
35
- private String bodyText ;
36
-
37
- /** The image resource which is displayed in the middle of the fragment, above the text */
38
- private @ DrawableRes int imageResId ;
39
-
40
- // private OnFragmentInteractionListener mListener;
31
+ private static final String POSITION = "POSITION" ;
32
+ private static final String BUTTON_TEXT = "BUTTON_TEXT" ;
41
33
42
34
public OnboardingFragment () {
43
35
// Required empty public constructor
@@ -53,22 +45,18 @@ public OnboardingFragment() {
53
45
* @return A new instance of fragment OnboardingFragment.
54
46
*/
55
47
// TODO: Rename and change types and number of parameters
56
- public static OnboardingFragment newInstance (OnboardingPage onboardingPage ) {
48
+ public static OnboardingFragment newInstance (OnboardingPage onboardingPage , int position ) {
57
49
OnboardingFragment fragment = new OnboardingFragment ();
58
50
Bundle args = new Bundle ();
59
51
args .putString (TITLE , onboardingPage .getTitle ());
60
52
args .putString (BODY_TEXT , onboardingPage .getBodyText ());
61
53
args .putInt (IMAGE_RESOURCE_ID , onboardingPage .getImageResId ());
54
+ args .putInt (POSITION , position );
55
+ args .putString (BUTTON_TEXT , onboardingPage .getButtonText ());
62
56
fragment .setArguments (args );
63
57
return fragment ;
64
58
}
65
59
66
- @ Override
67
- public void onAttach (Context context ) {
68
- super .onAttach (context );
69
- //TODO get a reference to the activity here
70
- }
71
-
72
60
@ Override
73
61
public View onCreateView (LayoutInflater inflater , ViewGroup container ,
74
62
Bundle savedInstanceState ) {
@@ -77,74 +65,78 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
77
65
// Inflate the layout for this fragment
78
66
Bundle bundle = getArguments ();
79
67
80
- title = bundle .getString (TITLE ,null );
81
- bodyText = bundle .getString (BODY_TEXT , null );
82
- imageResId = bundle .getInt (IMAGE_RESOURCE_ID ,-1 );
68
+ /* The title which is displayed at the top of the fragment */
69
+ String title = bundle .getString (TITLE , null );
70
+ /* The body text which is displayed in the middle of the fragment */
71
+ String bodyText = bundle .getString (BODY_TEXT , null );
72
+ /* The image resource which is displayed in the middle of the fragment, above the text */
73
+ int imageResId = bundle .getInt (IMAGE_RESOURCE_ID , -1 );
74
+ /* The position that the fragment is in adapter */
75
+ final int position = bundle .getInt (POSITION , 0 );
76
+ /* The button text (if the user set any) */
77
+ String buttonText = bundle .getString (BUTTON_TEXT , null );
83
78
84
79
TextView titleTextView = (TextView ) view .findViewById (R .id .onboarding_fragment_title );
85
80
TextView bodyTextView = (TextView ) view .findViewById (R .id .onboarding_fragment_body_text );
86
81
ImageView imageView = (ImageView ) view .findViewById (R .id .onboarding_fragment_image );
82
+ Button button = (Button ) view .findViewById (R .id .onboarding_fragment_button );
87
83
88
84
//Set the title
89
- if (title !=null ) {
85
+ if (title !=null ) {
90
86
titleTextView .setText (title );
91
87
} else {
92
88
titleTextView .setVisibility (View .GONE );
93
89
}
94
90
95
91
//Set the body text
96
- if (bodyText !=null ) {
92
+ if (bodyText !=null ) {
97
93
bodyTextView .setText (bodyText );
98
94
} else {
99
95
bodyTextView .setVisibility (View .GONE );
100
96
}
101
97
102
98
//Set the image
103
- if (imageResId !=-1 ) {
99
+ if (imageResId !=-1 ) {
104
100
imageView .setImageResource (imageResId );
105
101
} else {
106
102
imageView .setVisibility (View .GONE );
107
103
}
108
104
105
+ //Set the button text and link it to the method
106
+ if (buttonText !=null ) {
107
+ button .setText (buttonText );
108
+ button .setOnClickListener (new View .OnClickListener () {
109
+ @ Override
110
+ public void onClick (View v ) {
111
+ clickOnboardingButton (position );
112
+ }
113
+ });
114
+ } else {
115
+ button .setVisibility (View .GONE );
116
+ }
117
+
109
118
return view ;
110
119
}
111
120
112
- // // TODO: Rename method, update argument and hook method into UI event
113
- // public void onButtonPressed(Uri uri) {
114
- // if (mListener != null) {
115
- // mListener.onFragmentInteraction(uri);
116
- // }
117
- // }
118
- //
119
- // @Override
120
- // public void onAttach(Context context) {
121
- // super.onAttach(context);
122
- // if (context instanceof OnFragmentInteractionListener) {
123
- // mListener = (OnFragmentInteractionListener) context;
124
- // } else {
125
- // throw new RuntimeException(context.toString()
126
- // + " must implement OnFragmentInteractionListener");
127
- // }
128
- // }
129
- //
130
- // @Override
131
- // public void onDetach() {
132
- // super.onDetach();
133
- // mListener = null;
134
- // }
135
- //
136
- // /**
137
- // * This interface must be implemented by activities that contain this
138
- // * fragment to allow an interaction in this fragment to be communicated
139
- // * to the activity and potentially other fragments contained in that
140
- // * activity.
141
- // * <p/>
142
- // * See the Android Training lesson <a href=
143
- // * "http://developer.android.com/training/basics/fragments/communicating.html"
144
- // * >Communicating with Other Fragments</a> for more information.
145
- // */
146
- // public interface OnFragmentInteractionListener {
147
- // // TODO: Update argument type and name
148
- // void onFragmentInteraction(Uri uri);
149
- // }
121
+ private onOnboardingButtonClickListener buttonClickListener ;
122
+
123
+ private void clickOnboardingButton (int position ) {
124
+ buttonClickListener .onOnboardingClick (position );
125
+ }
126
+
127
+ @ Override
128
+ public void onAttach (Context context ) {
129
+ super .onAttach (context );
130
+ buttonClickListener = (onOnboardingButtonClickListener ) context ;
131
+ }
132
+
133
+ @ Override
134
+ public void onDetach () {
135
+ super .onDetach ();
136
+ buttonClickListener = null ;
137
+ }
138
+
139
+ public interface onOnboardingButtonClickListener {
140
+ void onOnboardingClick (int position );
141
+ }
150
142
}
0 commit comments