无法在RecyclerView.Adapter中设置侦听器,投射问题

时间:2019-07-11 12:17:04

标签: android xamarin xamarin.android recycler-adapter

从早上开始,我一直在尝试运行此代码。我有一个DialogFragment,其中包含一个回收站视图,它是适配器。当我触摸适配器中的某个项目时,它会更改颜色并为该项目设置一个标志。我希望听众将触摸连接到对话框,以便能够提取数据。我有以下代码...

using System;
using System.Collections.Generic;
using Android;
using Android.Support.V7.Widget;
using Android.Views;
using System.Linq;
using RecordMilk.ViewHolders;
using RecordMilk.Classes;
using Newtonsoft.Json;
using RecordMilk.Dialogs;

public class PregnantAnimalAdapter : RecyclerView.Adapter
{
public PregnantAnimalAdapter pregnantAnimalAdapter;
public List<Animal> mAnimals;
public Herd passedHerd;
public OnFinishClick callback;
public OnItemClick itemCallback;
string[] animalTags;
//public event EventHandler<int> ItemClick;

public interface OnFinishClick
{
    void OnFinishClick(List<Animal> animals);
}

public interface OnItemClick
{
    void OnItemClick(List<Animal> animals);
}

public PregnantAnimalAdapter(string SerializedString)
{
    passedHerd = JsonConvert.DeserializeObject<Herd>(SerializedString);

    List<Animal> animals = passedHerd.Animals.ToList();
    animalTags = animals.Select(x => x.FreezeBrand).ToArray();

    var size = from x in animalTags
               orderby x.Length, x
               select x;

    animalTags = size.ToArray();

    mAnimals = animals;
}



public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
    View itemView = LayoutInflater.From(parent.Context).
                                  Inflate(global::RecordMilk.Resource.Layout.PregnancyItem, parent, false);

    try
    {
        // Getting Casting Error here...
        itemCallback = (OnItemClick)parent.Context;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    PregnantAnimalViewHolder vh = new PregnantAnimalViewHolder(itemView);

    vh.mBackground.Click += (object sender, EventArgs e) =>
    {
        Animal pregAnimal = mAnimals.Find(x => x.FreezeBrand == animalTags[vh.AdapterPosition]);

        pregAnimal.Pregnant = !pregAnimal.Pregnant;

        if (pregAnimal.Pregnant)
        {
            vh.mBackground.SetBackgroundResource(RecordMilk.Resource.Color.munster_yellow);
        }
        else
        {
            vh.mBackground.SetBackgroundResource(Resource.Color.White);
        }
        NotifyDataSetChanged();

        itemCallback.OnItemClick(mAnimals);
    };

    return vh;
}

public override void OnAttachedToRecyclerView(RecyclerView recyclerView)
{
    base.OnAttachedToRecyclerView(recyclerView);

}

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{

    PregnantAnimalViewHolder vh = holder as PregnantAnimalViewHolder;

    vh.mAnimalTag.Text = animalTags[position];

    if (mAnimals.Find(x => x.FreezeBrand == animalTags[position]).Pregnant == true)
    {
        vh.mBackground.SetBackgroundResource(RecordMilk.Resource.Color.munster_yellow);
    }
    else
    {
        vh.mBackground.SetBackgroundResource(Resource.Color.White);
    }
}

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

我得到的错误是:

  

System.InvalidCastException:指定的强制转换无效。     在怀孕动物适配器.OnCreateViewHolder(Android.Views.ViewGroup父级,System.Int32 viewType)上

我以前在一个Activity中有此代码,并按预期工作。但是我需要将功能放到DialogFragment中,这是问题开始显现的地方。

0 个答案:

没有答案