我试图为RecyclerView中的每个CardView生成一个调色板,并根据生成的调色板为卡着色。
但是,由于一些奇怪的原因,只有RecyclerView的最后两个CardView才会变色。
这是我的代码(它在C#中,使用Xamarin,但它与Java代码几乎相同):
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; }
}
}
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;
}
有人可以帮我找出为什么只有最后两件物品上色了吗?
谢谢!
更新 开始赏金。急需帮助。
答案 0 :(得分:2)
我更改了一些内容,这里是完整的适配器,我没有对任何代码进行优化,但它确实有效。
总结如下解决了这个问题:
var palette = Palette.From(((BitmapDrawable)myHolder.mCoverImage.Drawable).Bitmap).MaximumColorCount(16).Generate(new PalleteGeneration(myHolder));
这是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);
}
}
获取完整的RecyclerAdapter here。
最终结果: