From b7c57025a7395900f70da230b5a713d2884cf048 Mon Sep 17 00:00:00 2001 From: Craig Dunn Date: Fri, 13 Dec 2019 10:39:12 -0800 Subject: [PATCH] [recyclerview] better OO, formatting (#310) fixes https://github.com/MicrosoftDocs/xamarin-docs/issues/1372 --- .../RecyclerViewer/MainActivity.cs | 130 ++++++------- .../RecyclerViewer/PhotoAlbum.cs | 184 +++++++++--------- .../Properties/AndroidManifest.xml | 9 +- .../RecyclerViewer/RecyclerViewer.csproj | 3 +- 4 files changed, 159 insertions(+), 167 deletions(-) diff --git a/android5.0/RecyclerViewer/RecyclerViewer/MainActivity.cs b/android5.0/RecyclerViewer/RecyclerViewer/MainActivity.cs index 89be8e045..a48de99fa 100644 --- a/android5.0/RecyclerViewer/RecyclerViewer/MainActivity.cs +++ b/android5.0/RecyclerViewer/RecyclerViewer/MainActivity.cs @@ -9,61 +9,61 @@ using System.Collections.Generic; namespace RecyclerViewer -{ - [Activity (Label = "RecyclerViewer", MainLauncher = true, Icon = "@drawable/icon", - Theme = "@android:style/Theme.Material.Light.DarkActionBar")] - public class MainActivity : Activity - { - // RecyclerView instance that displays the photo album: - RecyclerView mRecyclerView; - - // Layout manager that lays out each card in the RecyclerView: - RecyclerView.LayoutManager mLayoutManager; - - // Adapter that accesses the data set (a photo album): - PhotoAlbumAdapter mAdapter; +{ + [Activity(Label = "RecyclerViewer", MainLauncher = true, Icon = "@drawable/icon", + Theme = "@android:style/Theme.Material.Light.DarkActionBar")] + public class MainActivity : Activity + { + // RecyclerView instance that displays the photo album: + RecyclerView mRecyclerView; + + // Layout manager that lays out each card in the RecyclerView: + RecyclerView.LayoutManager mLayoutManager; + + // Adapter that accesses the data set (a photo album): + PhotoAlbumAdapter mAdapter; // Photo album that is managed by the adapter: - PhotoAlbum mPhotoAlbum; - - protected override void OnCreate (Bundle bundle) - { - base.OnCreate (bundle); + PhotoAlbum mPhotoAlbum; + + protected override void OnCreate(Bundle bundle) + { + base.OnCreate(bundle); // Instantiate the photo album: - mPhotoAlbum = new PhotoAlbum(); - - // Set our view from the "main" layout resource: - SetContentView (Resource.Layout.Main); - - // Get our RecyclerView layout: - mRecyclerView = FindViewById (Resource.Id.recyclerView); - - //............................................................ - // Layout Manager Setup: - - // Use the built-in linear layout manager: - mLayoutManager = new LinearLayoutManager (this); + mPhotoAlbum = new PhotoAlbum(); + + // Set our view from the "main" layout resource: + SetContentView(Resource.Layout.Main); + + // Get our RecyclerView layout: + mRecyclerView = FindViewById(Resource.Id.recyclerView); + + //............................................................ + // Layout Manager Setup: + + // Use the built-in linear layout manager: + mLayoutManager = new LinearLayoutManager(this); // Or use the built-in grid layout manager (two horizontal rows): // mLayoutManager = new GridLayoutManager // (this, 2, GridLayoutManager.Horizontal, false); // Plug the layout manager into the RecyclerView: - mRecyclerView.SetLayoutManager (mLayoutManager); - - //............................................................ - // Adapter Setup: - - // Create an adapter for the RecyclerView, and pass it the - // data set (the photo album) to manage: - mAdapter = new PhotoAlbumAdapter (mPhotoAlbum); + mRecyclerView.SetLayoutManager(mLayoutManager); + + //............................................................ + // Adapter Setup: + + // Create an adapter for the RecyclerView, and pass it the + // data set (the photo album) to manage: + mAdapter = new PhotoAlbumAdapter(mPhotoAlbum); // Register the item click handler (below) with the adapter: - mAdapter.ItemClick += OnItemClick; - - // Plug the adapter into the RecyclerView: - mRecyclerView.SetAdapter (mAdapter); + mAdapter.ItemClick += OnItemClick; + + // Plug the adapter into the RecyclerView: + mRecyclerView.SetAdapter(mAdapter); //............................................................ // Random Pick Button: @@ -84,17 +84,17 @@ protected override void OnCreate (Bundle bundle) mAdapter.NotifyItemChanged(0); mAdapter.NotifyItemChanged(idx); } - }; - } + }; + } // Handler for the item click event: - void OnItemClick (object sender, int position) + void OnItemClick(object sender, int position) { // Display a toast that briefly shows the enumeration of the selected photo: int photoNum = position + 1; Toast.MakeText(this, "This is photo number " + photoNum, ToastLength.Short).Show(); - } - } + } + } //---------------------------------------------------------------------- // VIEW HOLDER @@ -108,16 +108,16 @@ public class PhotoViewHolder : RecyclerView.ViewHolder public TextView Caption { get; private set; } // Get references to the views defined in the CardView layout. - public PhotoViewHolder (View itemView, Action listener) - : base (itemView) + public PhotoViewHolder(View itemView, Action listener) + : base(itemView) { // Locate and cache view references: - Image = itemView.FindViewById (Resource.Id.imageView); - Caption = itemView.FindViewById (Resource.Id.textView); + Image = itemView.FindViewById(Resource.Id.imageView); + Caption = itemView.FindViewById(Resource.Id.textView); // Detect user clicks on the item view and report which item // was clicked (by layout position) to the listener: - itemView.Click += (sender, e) => listener (base.LayoutPosition); + itemView.Click += (sender, e) => listener(base.LayoutPosition); } } @@ -131,37 +131,37 @@ public class PhotoAlbumAdapter : RecyclerView.Adapter public event EventHandler ItemClick; // Underlying data set (a photo album): - public PhotoAlbum mPhotoAlbum; + PhotoAlbum mPhotoAlbum; // Load the adapter with the data set (photo album) at construction time: - public PhotoAlbumAdapter (PhotoAlbum photoAlbum) + public PhotoAlbumAdapter(PhotoAlbum photoAlbum) { mPhotoAlbum = photoAlbum; } // Create a new photo CardView (invoked by the layout manager): - public override RecyclerView.ViewHolder - OnCreateViewHolder (ViewGroup parent, int viewType) + public override RecyclerView.ViewHolder + OnCreateViewHolder(ViewGroup parent, int viewType) { // Inflate the CardView for the photo: - View itemView = LayoutInflater.From (parent.Context). - Inflate (Resource.Layout.PhotoCardView, parent, false); + View itemView = LayoutInflater.From(parent.Context). + Inflate(Resource.Layout.PhotoCardView, parent, false); // Create a ViewHolder to find and hold these view references, and // register OnClick with the view holder: - PhotoViewHolder vh = new PhotoViewHolder (itemView, OnClick); + PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick); return vh; } // Fill in the contents of the photo card (invoked by the layout manager): - public override void - OnBindViewHolder (RecyclerView.ViewHolder holder, int position) + public override void + OnBindViewHolder(RecyclerView.ViewHolder holder, int position) { PhotoViewHolder vh = holder as PhotoViewHolder; // Set the ImageView and TextView in this ViewHolder's CardView // from this position in the photo album: - vh.Image.SetImageResource (mPhotoAlbum[position].PhotoID); + vh.Image.SetImageResource(mPhotoAlbum[position].PhotoID); vh.Caption.Text = mPhotoAlbum[position].Caption; } @@ -172,10 +172,10 @@ public override int ItemCount } // Raise an event when the item-click takes place: - void OnClick (int position) + void OnClick(int position) { if (ItemClick != null) - ItemClick (this, position); + ItemClick(this, position); } } } diff --git a/android5.0/RecyclerViewer/RecyclerViewer/PhotoAlbum.cs b/android5.0/RecyclerViewer/RecyclerViewer/PhotoAlbum.cs index 459e1a506..d3f98ff14 100644 --- a/android5.0/RecyclerViewer/RecyclerViewer/PhotoAlbum.cs +++ b/android5.0/RecyclerViewer/RecyclerViewer/PhotoAlbum.cs @@ -13,23 +13,17 @@ namespace RecyclerViewer // Photo: contains image resource ID and caption: public class Photo { - // Photo ID for this photo: - public int mPhotoID; - - // Caption text for this photo: - public string mCaption; - - // Return the ID of the photo: - public int PhotoID - { - get { return mPhotoID; } - } + public Photo(int id, string caption) + { + PhotoID = id; + Caption = caption; + } + + // Return the ID of the photo: + public int PhotoID { get; } // Return the Caption of the photo: - public string Caption - { - get { return mCaption; } - } + public string Caption { get; } } // Photo album: holds image resource IDs and caption: @@ -39,78 +33,78 @@ public class PhotoAlbum // a photo database: static Photo[] mBuiltInPhotos = { - new Photo { mPhotoID = Resource.Drawable.buckingham_guards, - mCaption = "Buckingham Palace" }, - new Photo { mPhotoID = Resource.Drawable.la_tour_eiffel, - mCaption = "The Eiffel Tower" }, - new Photo { mPhotoID = Resource.Drawable.louvre_1, - mCaption = "The Louvre" }, - new Photo { mPhotoID = Resource.Drawable.before_mobile_phones, - mCaption = "Before mobile phones" }, - new Photo { mPhotoID = Resource.Drawable.big_ben_1, - mCaption = "Big Ben skyline" }, - new Photo { mPhotoID = Resource.Drawable.big_ben_2, - mCaption = "Big Ben from below" }, - new Photo { mPhotoID = Resource.Drawable.london_eye, - mCaption = "The London Eye" }, - new Photo { mPhotoID = Resource.Drawable.eurostar, - mCaption = "Eurostar Train" }, - new Photo { mPhotoID = Resource.Drawable.arc_de_triomphe, - mCaption = "Arc de Triomphe" }, - new Photo { mPhotoID = Resource.Drawable.louvre_2, - mCaption = "Inside the Louvre" }, - new Photo { mPhotoID = Resource.Drawable.versailles_fountains, - mCaption = "Versailles fountains" }, - new Photo { mPhotoID = Resource.Drawable.modest_accomodations, - mCaption = "Modest accomodations" }, - new Photo { mPhotoID = Resource.Drawable.notre_dame, - mCaption = "Notre Dame" }, - new Photo { mPhotoID = Resource.Drawable.inside_notre_dame, - mCaption = "Inside Notre Dame" }, - new Photo { mPhotoID = Resource.Drawable.seine_river, - mCaption = "The Seine" }, - new Photo { mPhotoID = Resource.Drawable.rue_cler, - mCaption = "Rue Cler" }, - new Photo { mPhotoID = Resource.Drawable.champ_elysees, - mCaption = "The Avenue des Champs-Elysees" }, - new Photo { mPhotoID = Resource.Drawable.seine_barge, - mCaption = "Seine barge" }, - new Photo { mPhotoID = Resource.Drawable.versailles_gates, - mCaption = "Gates of Versailles" }, - new Photo { mPhotoID = Resource.Drawable.edinburgh_castle_2, - mCaption = "Edinburgh Castle" }, - new Photo { mPhotoID = Resource.Drawable.edinburgh_castle_1, - mCaption = "Edinburgh Castle up close" }, - new Photo { mPhotoID = Resource.Drawable.old_meets_new, - mCaption = "Old meets new" }, - new Photo { mPhotoID = Resource.Drawable.edinburgh_from_on_high, - mCaption = "Edinburgh from on high" }, - new Photo { mPhotoID = Resource.Drawable.edinburgh_station, - mCaption = "Edinburgh station" }, - new Photo { mPhotoID = Resource.Drawable.scott_monument, - mCaption = "Scott Monument" }, - new Photo { mPhotoID = Resource.Drawable.view_from_holyrood_park, - mCaption = "View from Holyrood Park" }, - new Photo { mPhotoID = Resource.Drawable.tower_of_london, - mCaption = "Outside the Tower of London" }, - new Photo { mPhotoID = Resource.Drawable.tower_visitors, - mCaption = "Tower of London visitors" }, - new Photo { mPhotoID = Resource.Drawable.one_o_clock_gun, - mCaption = "One O'Clock Gun" }, - new Photo { mPhotoID = Resource.Drawable.victoria_albert, - mCaption = "Victoria and Albert Museum" }, - new Photo { mPhotoID = Resource.Drawable.royal_mile, - mCaption = "The Royal Mile" }, - new Photo { mPhotoID = Resource.Drawable.museum_and_castle, - mCaption = "Edinburgh Museum and Castle" }, - new Photo { mPhotoID = Resource.Drawable.portcullis_gate, - mCaption = "Portcullis Gate" }, - new Photo { mPhotoID = Resource.Drawable.to_notre_dame, - mCaption = "Left or right?" }, - new Photo { mPhotoID = Resource.Drawable.pompidou_centre, - mCaption = "Pompidou Centre" }, - new Photo { mPhotoID = Resource.Drawable.heres_lookin_at_ya, - mCaption = "Here's Lookin' at Ya!" }, + new Photo ( Resource.Drawable.buckingham_guards, + "Buckingham Palace" ), + new Photo ( Resource.Drawable.la_tour_eiffel, + "The Eiffel Tower" ), + new Photo ( Resource.Drawable.louvre_1, + "The Louvre" ), + new Photo ( Resource.Drawable.before_mobile_phones, + "Before mobile phones" ), + new Photo ( Resource.Drawable.big_ben_1, + "Big Ben skyline" ), + new Photo ( Resource.Drawable.big_ben_2, + "Big Ben from below" ), + new Photo ( Resource.Drawable.london_eye, + "The London Eye" ), + new Photo ( Resource.Drawable.eurostar, + "Eurostar Train" ), + new Photo ( Resource.Drawable.arc_de_triomphe, + "Arc de Triomphe" ), + new Photo ( Resource.Drawable.louvre_2, + "Inside the Louvre" ), + new Photo ( Resource.Drawable.versailles_fountains, + "Versailles fountains" ), + new Photo ( Resource.Drawable.modest_accomodations, + "Modest accomodations" ), + new Photo ( Resource.Drawable.notre_dame, + "Notre Dame" ), + new Photo ( Resource.Drawable.inside_notre_dame, + "Inside Notre Dame" ), + new Photo ( Resource.Drawable.seine_river, + "The Seine" ), + new Photo ( Resource.Drawable.rue_cler, + "Rue Cler" ), + new Photo ( Resource.Drawable.champ_elysees, + "The Avenue des Champs-Elysees" ), + new Photo ( Resource.Drawable.seine_barge, + "Seine barge" ), + new Photo ( Resource.Drawable.versailles_gates, + "Gates of Versailles" ), + new Photo ( Resource.Drawable.edinburgh_castle_2, + "Edinburgh Castle" ), + new Photo ( Resource.Drawable.edinburgh_castle_1, + "Edinburgh Castle up close" ), + new Photo ( Resource.Drawable.old_meets_new, + "Old meets new" ), + new Photo ( Resource.Drawable.edinburgh_from_on_high, + "Edinburgh from on high" ), + new Photo ( Resource.Drawable.edinburgh_station, + "Edinburgh station" ), + new Photo ( Resource.Drawable.scott_monument, + "Scott Monument" ), + new Photo ( Resource.Drawable.view_from_holyrood_park, + "View from Holyrood Park" ), + new Photo ( Resource.Drawable.tower_of_london, + "Outside the Tower of London" ), + new Photo ( Resource.Drawable.tower_visitors, + "Tower of London visitors" ), + new Photo ( Resource.Drawable.one_o_clock_gun, + "One O'Clock Gun" ), + new Photo ( Resource.Drawable.victoria_albert, + "Victoria and Albert Museum" ), + new Photo ( Resource.Drawable.royal_mile, + "The Royal Mile" ), + new Photo ( Resource.Drawable.museum_and_castle, + "Edinburgh Museum and Castle" ), + new Photo ( Resource.Drawable.portcullis_gate, + "Portcullis Gate" ), + new Photo ( Resource.Drawable.to_notre_dame, + "Left or right?" ), + new Photo ( Resource.Drawable.pompidou_centre, + "Pompidou Centre" ), + new Photo ( Resource.Drawable.heres_lookin_at_ya, + "Here's Lookin' at Ya!" ), }; // Array of photos that make up the album: @@ -121,20 +115,20 @@ public class PhotoAlbum // Create an instance copy of the built-in photo list and // create the random number generator: - public PhotoAlbum () + public PhotoAlbum() { mPhotos = mBuiltInPhotos; mRandom = new Random(); } // Return the number of photos in the photo album: - public int NumPhotos - { - get { return mPhotos.Length; } + public int NumPhotos + { + get { return mPhotos.Length; } } // Indexer (read only) for accessing a photo: - public Photo this[int i] + public Photo this[int i] { get { return mPhotos[i]; } } @@ -158,9 +152,9 @@ public int RandomSwap() } // Shuffle the order of the photos: - public void Shuffle () - { - // Use the Fisher-Yates shuffle algorithm: + public void Shuffle() + { + // Use the Fisher-Yates shuffle algorithm: for (int idx = 0; idx < mPhotos.Length; ++idx) { // Save the photo at idx: diff --git a/android5.0/RecyclerViewer/RecyclerViewer/Properties/AndroidManifest.xml b/android5.0/RecyclerViewer/RecyclerViewer/Properties/AndroidManifest.xml index 0e482553e..9a812f7bc 100644 --- a/android5.0/RecyclerViewer/RecyclerViewer/Properties/AndroidManifest.xml +++ b/android5.0/RecyclerViewer/RecyclerViewer/Properties/AndroidManifest.xml @@ -1,6 +1,5 @@  - - - - - + + + + \ No newline at end of file diff --git a/android5.0/RecyclerViewer/RecyclerViewer/RecyclerViewer.csproj b/android5.0/RecyclerViewer/RecyclerViewer/RecyclerViewer.csproj index 8dbf62af5..62ff00ded 100644 --- a/android5.0/RecyclerViewer/RecyclerViewer/RecyclerViewer.csproj +++ b/android5.0/RecyclerViewer/RecyclerViewer/RecyclerViewer.csproj @@ -12,7 +12,6 @@ Resource Resources\Resource.designer.cs True - False RecyclerViewer v8.1 Properties\AndroidManifest.xml @@ -146,7 +145,7 @@ - + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.