Skip to content

Commit b7c5702

Browse files
authored
[recyclerview] better OO, formatting (#310)
fixes https://github.com/MicrosoftDocs/xamarin-docs/issues/1372
1 parent 8833a66 commit b7c5702

File tree

4 files changed

+159
-167
lines changed

4 files changed

+159
-167
lines changed

android5.0/RecyclerViewer/RecyclerViewer/MainActivity.cs

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,61 @@
99
using System.Collections.Generic;
1010

1111
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;
2525

2626
// 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);
3232

3333
// 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);
4747

4848
// Or use the built-in grid layout manager (two horizontal rows):
4949
// mLayoutManager = new GridLayoutManager
5050
// (this, 2, GridLayoutManager.Horizontal, false);
5151

5252
// 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);
6161

6262
// 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);
6767

6868
//............................................................
6969
// Random Pick Button:
@@ -84,17 +84,17 @@ protected override void OnCreate (Bundle bundle)
8484
mAdapter.NotifyItemChanged(0);
8585
mAdapter.NotifyItemChanged(idx);
8686
}
87-
};
88-
}
87+
};
88+
}
8989

9090
// Handler for the item click event:
91-
void OnItemClick (object sender, int position)
91+
void OnItemClick(object sender, int position)
9292
{
9393
// Display a toast that briefly shows the enumeration of the selected photo:
9494
int photoNum = position + 1;
9595
Toast.MakeText(this, "This is photo number " + photoNum, ToastLength.Short).Show();
96-
}
97-
}
96+
}
97+
}
9898

9999
//----------------------------------------------------------------------
100100
// VIEW HOLDER
@@ -108,16 +108,16 @@ public class PhotoViewHolder : RecyclerView.ViewHolder
108108
public TextView Caption { get; private set; }
109109

110110
// 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)
113113
{
114114
// 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);
117117

118118
// Detect user clicks on the item view and report which item
119119
// was clicked (by layout position) to the listener:
120-
itemView.Click += (sender, e) => listener (base.LayoutPosition);
120+
itemView.Click += (sender, e) => listener(base.LayoutPosition);
121121
}
122122
}
123123

@@ -131,37 +131,37 @@ public class PhotoAlbumAdapter : RecyclerView.Adapter
131131
public event EventHandler<int> ItemClick;
132132

133133
// Underlying data set (a photo album):
134-
public PhotoAlbum mPhotoAlbum;
134+
PhotoAlbum mPhotoAlbum;
135135

136136
// Load the adapter with the data set (photo album) at construction time:
137-
public PhotoAlbumAdapter (PhotoAlbum photoAlbum)
137+
public PhotoAlbumAdapter(PhotoAlbum photoAlbum)
138138
{
139139
mPhotoAlbum = photoAlbum;
140140
}
141141

142142
// 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)
145145
{
146146
// 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);
149149

150150
// Create a ViewHolder to find and hold these view references, and
151151
// register OnClick with the view holder:
152-
PhotoViewHolder vh = new PhotoViewHolder (itemView, OnClick);
152+
PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick);
153153
return vh;
154154
}
155155

156156
// 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)
159159
{
160160
PhotoViewHolder vh = holder as PhotoViewHolder;
161161

162162
// Set the ImageView and TextView in this ViewHolder's CardView
163163
// from this position in the photo album:
164-
vh.Image.SetImageResource (mPhotoAlbum[position].PhotoID);
164+
vh.Image.SetImageResource(mPhotoAlbum[position].PhotoID);
165165
vh.Caption.Text = mPhotoAlbum[position].Caption;
166166
}
167167

@@ -172,10 +172,10 @@ public override int ItemCount
172172
}
173173

174174
// Raise an event when the item-click takes place:
175-
void OnClick (int position)
175+
void OnClick(int position)
176176
{
177177
if (ItemClick != null)
178-
ItemClick (this, position);
178+
ItemClick(this, position);
179179
}
180180
}
181181
}

0 commit comments

Comments
 (0)