Skip to content

Commit

Permalink
[recyclerview] better OO, formatting (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
conceptdev authored Dec 13, 2019
1 parent 8833a66 commit b7c5702
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 167 deletions.
130 changes: 65 additions & 65 deletions android5.0/RecyclerViewer/RecyclerViewer/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecyclerView> (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<RecyclerView>(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:
Expand All @@ -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
Expand All @@ -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<int> listener)
: base (itemView)
public PhotoViewHolder(View itemView, Action<int> listener)
: base(itemView)
{
// Locate and cache view references:
Image = itemView.FindViewById<ImageView> (Resource.Id.imageView);
Caption = itemView.FindViewById<TextView> (Resource.Id.textView);
Image = itemView.FindViewById<ImageView>(Resource.Id.imageView);
Caption = itemView.FindViewById<TextView>(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);
}
}

Expand All @@ -131,37 +131,37 @@ public class PhotoAlbumAdapter : RecyclerView.Adapter
public event EventHandler<int> 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;
}

Expand All @@ -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);
}
}
}
Loading

0 comments on commit b7c5702

Please sign in to comment.