9
9
using System . Collections . Generic ;
10
10
11
11
namespace RecyclerViewer
12
- {
13
- [ Activity ( Label = "RecyclerViewer" , MainLauncher = true , Icon = "@drawable/icon" ,
14
- Theme = "@android:style/Theme.Material.Light.DarkActionBar" ) ]
15
- public class MainActivity : Activity
16
- {
17
- // RecyclerView instance that displays the photo album:
18
- RecyclerView mRecyclerView ;
19
-
20
- // Layout manager that lays out each card in the RecyclerView:
21
- RecyclerView . LayoutManager mLayoutManager ;
22
-
23
- // Adapter that accesses the data set (a photo album):
24
- PhotoAlbumAdapter mAdapter ;
12
+ {
13
+ [ Activity ( Label = "RecyclerViewer" , MainLauncher = true , Icon = "@drawable/icon" ,
14
+ Theme = "@android:style/Theme.Material.Light.DarkActionBar" ) ]
15
+ public class MainActivity : Activity
16
+ {
17
+ // RecyclerView instance that displays the photo album:
18
+ RecyclerView mRecyclerView ;
19
+
20
+ // Layout manager that lays out each card in the RecyclerView:
21
+ RecyclerView . LayoutManager mLayoutManager ;
22
+
23
+ // Adapter that accesses the data set (a photo album):
24
+ PhotoAlbumAdapter mAdapter ;
25
25
26
26
// Photo album that is managed by the adapter:
27
- PhotoAlbum mPhotoAlbum ;
28
-
29
- protected override void OnCreate ( Bundle bundle )
30
- {
31
- base . OnCreate ( bundle ) ;
27
+ PhotoAlbum mPhotoAlbum ;
28
+
29
+ protected override void OnCreate ( Bundle bundle )
30
+ {
31
+ base . OnCreate ( bundle ) ;
32
32
33
33
// Instantiate the photo album:
34
- mPhotoAlbum = new PhotoAlbum ( ) ;
35
-
36
- // Set our view from the "main" layout resource:
37
- SetContentView ( Resource . Layout . Main ) ;
38
-
39
- // Get our RecyclerView layout:
40
- mRecyclerView = FindViewById < RecyclerView > ( Resource . Id . recyclerView ) ;
41
-
42
- //............................................................
43
- // Layout Manager Setup:
44
-
45
- // Use the built-in linear layout manager:
46
- mLayoutManager = new LinearLayoutManager ( this ) ;
34
+ mPhotoAlbum = new PhotoAlbum ( ) ;
35
+
36
+ // Set our view from the "main" layout resource:
37
+ SetContentView ( Resource . Layout . Main ) ;
38
+
39
+ // Get our RecyclerView layout:
40
+ mRecyclerView = FindViewById < RecyclerView > ( Resource . Id . recyclerView ) ;
41
+
42
+ //............................................................
43
+ // Layout Manager Setup:
44
+
45
+ // Use the built-in linear layout manager:
46
+ mLayoutManager = new LinearLayoutManager ( this ) ;
47
47
48
48
// Or use the built-in grid layout manager (two horizontal rows):
49
49
// mLayoutManager = new GridLayoutManager
50
50
// (this, 2, GridLayoutManager.Horizontal, false);
51
51
52
52
// Plug the layout manager into the RecyclerView:
53
- mRecyclerView . SetLayoutManager ( mLayoutManager ) ;
54
-
55
- //............................................................
56
- // Adapter Setup:
57
-
58
- // Create an adapter for the RecyclerView, and pass it the
59
- // data set (the photo album) to manage:
60
- mAdapter = new PhotoAlbumAdapter ( mPhotoAlbum ) ;
53
+ mRecyclerView . SetLayoutManager ( mLayoutManager ) ;
54
+
55
+ //............................................................
56
+ // Adapter Setup:
57
+
58
+ // Create an adapter for the RecyclerView, and pass it the
59
+ // data set (the photo album) to manage:
60
+ mAdapter = new PhotoAlbumAdapter ( mPhotoAlbum ) ;
61
61
62
62
// Register the item click handler (below) with the adapter:
63
- mAdapter . ItemClick += OnItemClick ;
64
-
65
- // Plug the adapter into the RecyclerView:
66
- mRecyclerView . SetAdapter ( mAdapter ) ;
63
+ mAdapter . ItemClick += OnItemClick ;
64
+
65
+ // Plug the adapter into the RecyclerView:
66
+ mRecyclerView . SetAdapter ( mAdapter ) ;
67
67
68
68
//............................................................
69
69
// Random Pick Button:
@@ -84,17 +84,17 @@ protected override void OnCreate (Bundle bundle)
84
84
mAdapter . NotifyItemChanged ( 0 ) ;
85
85
mAdapter . NotifyItemChanged ( idx ) ;
86
86
}
87
- } ;
88
- }
87
+ } ;
88
+ }
89
89
90
90
// Handler for the item click event:
91
- void OnItemClick ( object sender , int position )
91
+ void OnItemClick ( object sender , int position )
92
92
{
93
93
// Display a toast that briefly shows the enumeration of the selected photo:
94
94
int photoNum = position + 1 ;
95
95
Toast . MakeText ( this , "This is photo number " + photoNum , ToastLength . Short ) . Show ( ) ;
96
- }
97
- }
96
+ }
97
+ }
98
98
99
99
//----------------------------------------------------------------------
100
100
// VIEW HOLDER
@@ -108,16 +108,16 @@ public class PhotoViewHolder : RecyclerView.ViewHolder
108
108
public TextView Caption { get ; private set ; }
109
109
110
110
// Get references to the views defined in the CardView layout.
111
- public PhotoViewHolder ( View itemView , Action < int > listener )
112
- : base ( itemView )
111
+ public PhotoViewHolder ( View itemView , Action < int > listener )
112
+ : base ( itemView )
113
113
{
114
114
// Locate and cache view references:
115
- Image = itemView . FindViewById < ImageView > ( Resource . Id . imageView ) ;
116
- Caption = itemView . FindViewById < TextView > ( Resource . Id . textView ) ;
115
+ Image = itemView . FindViewById < ImageView > ( Resource . Id . imageView ) ;
116
+ Caption = itemView . FindViewById < TextView > ( Resource . Id . textView ) ;
117
117
118
118
// Detect user clicks on the item view and report which item
119
119
// was clicked (by layout position) to the listener:
120
- itemView . Click += ( sender , e ) => listener ( base . LayoutPosition ) ;
120
+ itemView . Click += ( sender , e ) => listener ( base . LayoutPosition ) ;
121
121
}
122
122
}
123
123
@@ -131,37 +131,37 @@ public class PhotoAlbumAdapter : RecyclerView.Adapter
131
131
public event EventHandler < int > ItemClick ;
132
132
133
133
// Underlying data set (a photo album):
134
- public PhotoAlbum mPhotoAlbum ;
134
+ PhotoAlbum mPhotoAlbum ;
135
135
136
136
// Load the adapter with the data set (photo album) at construction time:
137
- public PhotoAlbumAdapter ( PhotoAlbum photoAlbum )
137
+ public PhotoAlbumAdapter ( PhotoAlbum photoAlbum )
138
138
{
139
139
mPhotoAlbum = photoAlbum ;
140
140
}
141
141
142
142
// Create a new photo CardView (invoked by the layout manager):
143
- public override RecyclerView . ViewHolder
144
- OnCreateViewHolder ( ViewGroup parent , int viewType )
143
+ public override RecyclerView . ViewHolder
144
+ OnCreateViewHolder ( ViewGroup parent , int viewType )
145
145
{
146
146
// Inflate the CardView for the photo:
147
- View itemView = LayoutInflater . From ( parent . Context ) .
148
- Inflate ( Resource . Layout . PhotoCardView , parent , false ) ;
147
+ View itemView = LayoutInflater . From ( parent . Context ) .
148
+ Inflate ( Resource . Layout . PhotoCardView , parent , false ) ;
149
149
150
150
// Create a ViewHolder to find and hold these view references, and
151
151
// register OnClick with the view holder:
152
- PhotoViewHolder vh = new PhotoViewHolder ( itemView , OnClick ) ;
152
+ PhotoViewHolder vh = new PhotoViewHolder ( itemView , OnClick ) ;
153
153
return vh ;
154
154
}
155
155
156
156
// Fill in the contents of the photo card (invoked by the layout manager):
157
- public override void
158
- OnBindViewHolder ( RecyclerView . ViewHolder holder , int position )
157
+ public override void
158
+ OnBindViewHolder ( RecyclerView . ViewHolder holder , int position )
159
159
{
160
160
PhotoViewHolder vh = holder as PhotoViewHolder ;
161
161
162
162
// Set the ImageView and TextView in this ViewHolder's CardView
163
163
// from this position in the photo album:
164
- vh . Image . SetImageResource ( mPhotoAlbum [ position ] . PhotoID ) ;
164
+ vh . Image . SetImageResource ( mPhotoAlbum [ position ] . PhotoID ) ;
165
165
vh . Caption . Text = mPhotoAlbum [ position ] . Caption ;
166
166
}
167
167
@@ -172,10 +172,10 @@ public override int ItemCount
172
172
}
173
173
174
174
// Raise an event when the item-click takes place:
175
- void OnClick ( int position )
175
+ void OnClick ( int position )
176
176
{
177
177
if ( ItemClick != null )
178
- ItemClick ( this , position ) ;
178
+ ItemClick ( this , position ) ;
179
179
}
180
180
}
181
181
}
0 commit comments