Skip to content

Instantly share code, notes, and snippets.

@jzeferino
Last active June 18, 2016 15:43
Show Gist options
  • Save jzeferino/70ed3dfd96dfd14e3584092debc3af78 to your computer and use it in GitHub Desktop.
Save jzeferino/70ed3dfd96dfd14e3584092debc3af78 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V7.Widget;
using Android.Animation;
using Android.Support.V4.App;
using Android.Views.Animations;
using Android.Graphics.Drawables;
using Android.Graphics;
using Android.Support.V7.Graphics;
using System.Runtime.Serialization.Formatters;
namespace ToratHamachane
{
public class RecyclerAdapter : RecyclerView.Adapter
{
private List<Book> mBooks;
private RecyclerView mRecyclerView;
private Context mContext;
private int mCurrentPosition = -1;
public RecyclerAdapter(List<Book> books, RecyclerView recyclerView, Context context)
{
mBooks = books;
mRecyclerView = recyclerView;
mContext = context;
}
public class MyView : RecyclerView.ViewHolder
{
public View mMainView { get; set; }
public TextView mTitle { get; set; }
public ImageView mCoverImage { get; set; }
public int mCoverImageResourceID { get; set; }
public CardView mCard { get; set; }
public LinearLayout TitleBackground { get; set; }
public MyView(View view) : base(view)
{
mMainView = view;
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View bookItem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.item_book, parent, false);
TextView bookTitle = bookItem.FindViewById<TextView>(Resource.Id.bookTitle);
ImageView coverImage = bookItem.FindViewById<ImageView>(Resource.Id.coverImage);
CardView card = bookItem.FindViewById<CardView>(Resource.Id.card);
LinearLayout bookTitleLayout = bookItem.FindViewById<LinearLayout>(Resource.Id.bookTitleLayout);
MyView view = new MyView(bookItem) { mTitle = bookTitle, mCoverImage = coverImage, mCard = card, TitleBackground = bookTitleLayout };
return view;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var myHolder = holder as MyView;
myHolder.mMainView.Click += mMainView_Click;
myHolder.mTitle.Text = mBooks[position].Title;
myHolder.mCoverImage.SetImageDrawable(mBooks[position].Cover.Drawable);
if (position > mCurrentPosition)
{
int currentAnim = Resource.Animation.slide_left_to_right;
mCurrentPosition = position;
}
Palette.From(((BitmapDrawable)myHolder.mCoverImage.Drawable).Bitmap).MaximumColorCount(16).Generate(new PalleteGeneration(myHolder));
}
private void SetAnimation(View view, int currentAnim)
{
Animation anim = AnimationUtils.LoadAnimation(mContext, currentAnim);
view.StartAnimation(anim);
}
void mMainView_Click(object sender, EventArgs e)
{
int position = mRecyclerView.GetChildLayoutPosition((View)sender);
Console.WriteLine(mBooks[position].Title);
Intent intent;
intent = new Intent(mContext, typeof(BookReaderActivity));
intent.PutExtra("Title", mBooks[position].Title);
BitmapDrawable bitmapDrawable = ((BitmapDrawable)mBooks[position].Cover.Drawable);
Bitmap image = bitmapDrawable.Bitmap;
mContext.StartActivity(intent);
}
public class PalleteGeneration : Java.Lang.Object, Palette.IPaletteAsyncListener
{
private MyView _holder;
public PalleteGeneration(MyView holder)
{
_holder = holder;
}
public void OnGenerated(Palette palette)
{
if (palette == null)
return;
if (palette.LightVibrantSwatch != null)
{
var lightVibrant = new Color(palette.LightVibrantSwatch.Rgb);
_holder.mCard.SetCardBackgroundColor(lightVibrant);
}
else if (palette.LightMutedSwatch != null)
{
var lightVibrant = new Color(palette.LightMutedSwatch.Rgb);
_holder.mCard.SetCardBackgroundColor(lightVibrant);
}
if (palette.DarkVibrantSwatch != null)
{
var darkVibrant = new Color(palette.DarkVibrantSwatch.Rgb);
_holder.TitleBackground.SetBackgroundColor(darkVibrant);
}
else if (palette.DarkMutedSwatch != null)
{
var darkVibrant = new Color(palette.DarkMutedSwatch.Rgb);
_holder.TitleBackground.SetBackgroundColor(darkVibrant);
} }
}
public override int ItemCount
{
get { return mBooks.Count; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment