I'm trying to generate a Palette for each CardView I have inside a 
RecyclerView, and to color the card based on the Palette generated.
However, for some weird reason, only the last two CardViews of the 
RecyclerView get colored.


This is my code:


RecyclerAdapter:

public class RecyclerAdapter : RecyclerView.Adapter, 
Palette.IPaletteAsyncListener
    {
        private List<Book> mBooks;
        private RecyclerView mRecyclerView;
        private Context mContext;
        private int mCurrentPosition = -1;
        private bool isPaletteGenerated = false;
        MyView myHolder;
        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 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);

            MyView view = new MyView(bookItem) { mTitle = bookTitle, 
mCoverImage = coverImage, mCard = card};
            return view;
        }

        public override async void OnBindViewHolder(RecyclerView.ViewHolder 
holder, int position)
        {
            myHolder = holder as MyView;
            myHolder.mMainView.Click += mMainView_Click;
            myHolder.mTitle.Text = mBooks[position].Title;
            
myHolder.mCoverImage.SetImageDrawable(mBooks[position].Cover.Drawable);
            myHolder.mCoverImageResourceID = mBooks[position].imageResourceId;

            if (position > mCurrentPosition)
            {
                int currentAnim = Resource.Animation.slide_left_to_right;
                //SetAnimation(myHolder.mMainView, currentAnim);
                mCurrentPosition = position;
            }

            Bitmap photo = await 
BitmapFactory.DecodeResourceAsync(mContext.Resources, 
myHolder.mCoverImageResourceID);
            BitmapDrawable bitmapDrawable = 
((BitmapDrawable)mBooks[position].Cover.Drawable);
            Bitmap image = bitmapDrawable.Bitmap;

            var palette = 
Palette.From(photo).MaximumColorCount(16).Generate(this);


        }   

        public void OnGenerated(Palette palette)
        {

            if (palette == null)
                return;
            try
            {
                if (palette.LightVibrantSwatch != null)
                {
                    var lightVibrant = new 
Color(palette.LightVibrantSwatch.Rgb);
                    myHolder.mCard.SetCardBackgroundColor(lightVibrant);
                }
                if (palette.DarkVibrantSwatch != null)
                {
                    var darkVibrant = new Color(palette.DarkVibrantSwatch.Rgb);
                    myHolder.mCard.SetCardBackgroundColor(darkVibrant);
                }
            }
            catch (Exception ex)
            {

            }

        }

        public override int ItemCount
        {
            get { return mBooks.Count; }
        }
    }


Fragment in which the RecyclerView is inside:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState)
    {

        mRecyclerView = 
view.FindViewById<RecyclerView>(Resource.Id.booksRecyclerView);
        mBooks = new List<Book>();
        ImageView cover = new ImageView(Activity);

        cover.SetImageResource(Resource.Drawable.Torat_Hamachane1);
        mBooks.Add(new Book() { Title = "תורת המחנה א' - הלכות יום ויום", Cover 
= cover, imageResourceId = Resource.Drawable.Torat_Hamachane1}); 


        mLayoutManager = new GridLayoutManager(Activity, 2);
        mRecyclerView.SetForegroundGravity(GravityFlags.CenterHorizontal);
        mRecyclerView.SetLayoutManager(mLayoutManager);
        SimpleItemAnimator x = null;

        mRecyclerView.SetItemAnimator(x);
        mAdapter = new RecyclerAdapter(mBooks, mRecyclerView, Activity);
        mRecyclerView.SetAdapter(mAdapter);


        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.kitzurTHM);
        mBooks.Add(new Book() { Title = "קיצור תורת המחנה - הלכות יום ויום 
ושבת", Cover = cover, imageResourceId = Resource.Drawable.kitzurTHM });

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.halkasPurim);
        mBooks.Add(new Book() { Title = "הלכה כסדרה - הלכות פורים", Cover = 
cover, imageResourceId = Resource.Drawable.halkasPurim });

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.halkasPesach);
        mBooks.Add(new Book() { Title = "הלכה כסדרה - הלכות פסח", Cover = 
cover, imageResourceId= Resource.Drawable.halkasPesach });

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.netzach);
        mBooks.Add(new Book() { Title = "שו\"ת נצח יהודה", Cover = cover , 
imageResourceId = Resource.Drawable.netzach});

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.tankim);
        mBooks.Add(new Book() { Title = "טיפול בטנקים בשבת", Cover = cover , 
imageResourceId = Resource.Drawable.tankim});

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.tipulBeChalalim);
        mBooks.Add(new Book() { Title = "טיפול בחללים בשבת", Cover = cover , 
imageResourceId = Resource.Drawable.tipulBeChalalim});
        return view;

    }



Hope someone can please help me find out why only the last two items get 
colored.

Thanks!


-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/4267ad49-c9d3-4813-8f20-4575488bfefc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to