[英]Android Palette working only on some of RecyclerView items
I'm trying to generate a color Palette for each CardView I have inside a RecyclerView, and to color the card based on the Palette generated. 我正在尝试为RecyclerView中的每个CardView生成一个调色板,并根据生成的调色板为卡着色。
However, for some weird reason, only the last two CardViews of the RecyclerView get colored. 但是,由于一些奇怪的原因,只有RecyclerView的最后两个CardView才会变色。
This is my code (it's in C#, using Xamarin, but it's almost identical to the Java code): 这是我的代码(它在C#中,使用Xamarin,但它几乎与Java代码相同):
RecyclerAdapter: 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: RecyclerView所在的片段:
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;
}
Can someone can please help me find out why only the last two items get colored? 有人可以帮我找出为什么只有最后两个项目变色?
Thanks! 谢谢!
Update: Started bounty. 更新:开始赏金。 Desperate for help.
不顾一切地寻求帮助。
I changed up some things, here is the full adapter, i dind't optimized any of the code, but it works. 我改变了一些东西,这里是完整的适配器,我没有优化任何代码,但它的工作原理。
In summary solved it like this: 总结如下解决它:
var palette = Palette.From(((BitmapDrawable)myHolder.mCoverImage.Drawable).Bitmap).MaximumColorCount(16).Generate(new PalleteGeneration(myHolder));
Here is the PaleteGeneration: 这是PaleteGeneration:
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);
}
}
Get the full RecyclerAdapter here . 在这里获取完整的RecyclerAdapter。
Final result: 最后结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.