在RecyclerView中获取所选项的模型

时间:2016-08-04 11:12:54

标签: c# android xamarin xamarin.android

我是Xamarin.Android的新手。我正在构建一个社交平台类型的应用程序,我在查找RecyclerView中所选位置项的值时遇到了困难。场景是当用户登录时,他将从我使用RecyclerView成功显示的服务器获取他的新闻源。每个项目都有一个类似按钮(CheckBox)。当用户按下like按钮时,我必须发送一个API调用并传递feedID参数。我无法获得feedID。我的代码是:

型号:

public class NewsfeedCard
{
    public string FeedId { get; set; }
    public string UserPhoto { get; set; }
    public string UserName { get; set; }
    public string Address { get; set; }
    public string Time { get; set; }
    public string Description { get; set; }
    public int PersonThanked { get; set; }
    public int PersonThought { get; set; }
    public bool AlreadyLiked { get; set; }
    public bool AlreadyReported { get; set; }
}

ViewHolder:

public class NewsfeedViewHolder : RecyclerView.ViewHolder
{
    public ImageView Image { get; private set; }
    public TextView UserDesription { get; private set; }
    public TextView Username { get; private set; }
    public TextView UserAddress { get; private set; }
    public TextView PostTime { get; private set; }
    public TextView PersonThanked { get; private set; }
    public TextView PersonThought { get; private set; }
    public CheckBox LikeUnlikeCheckBox { get; set; }

    public NewsfeedViewHolder(View itemView, Action<int> listener) : base(itemView)
    {
        Image = itemView.FindViewById<ImageView>(Resource.Id.imageView);
        UserDesription = itemView.FindViewById<TextView>(Resource.Id.DescriptiontextView);
        Username = itemView.FindViewById<TextView>(Resource.Id.NametextView);
        UserAddress = itemView.FindViewById<TextView>(Resource.Id.AddresstextView);
        PostTime = itemView.FindViewById<TextView>(Resource.Id.TimetextView);
        PersonThanked = itemView.FindViewById<TextView>(Resource.Id.PersonThankedtextView);
        PersonThought = itemView.FindViewById<TextView>(Resource.Id.PersonThoughtstextView);
        LikeUnlikeCheckBox = itemView.FindViewById<CheckBox>(Resource.Id.likeButton);

        itemView.Click += (sender, e) => listener(LayoutPosition);
    }
}

适配器:

public class NewsfeedCardAdapter : RecyclerView.Adapter
{
    public event EventHandler<int> ItemClick;
    public NewsfeedCardData mNewsfeedCard;

    public NewsfeedCardAdapter(NewsfeedCardData newsfeedCard)
    {
        mNewsfeedCard = newsfeedCard;
    }

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

        NewsfeedViewHolder vh = new NewsfeedViewHolder(itemView, OnClick);
        return vh;
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        NewsfeedViewHolder vh = holder as NewsfeedViewHolder;

        var imageBitmap = GetImageBitmapFromUrl(mNewsfeedCard[position].UserPhoto);

        vh.Image.SetImageBitmap(imageBitmap);
        vh.UserDesription.Text = mNewsfeedCard[position].Description;
        vh.Username.Text = mNewsfeedCard[position].UserName;
        vh.UserAddress.Text = mNewsfeedCard[position].Address;
        vh.PostTime.Text = mNewsfeedCard[position].Time;

        if (mNewsfeedCard[position].PersonThanked > 0)
        {
            string personThankedText = "<font color=#36bc9b><b>" + mNewsfeedCard[position].PersonThanked + " Person" + "</b></font> <font color=#333333> thanked</font>";
            vh.PersonThanked.SetText(Html.FromHtml(personThankedText), TextView.BufferType.Spannable);
        }
        else
        {
            vh.PersonThanked.Text = "0 Person thanked";
        }

        if (mNewsfeedCard[position].PersonThought > 0)
        {
            string personThoughtText = "<font color=#36bc9b><b>" + mNewsfeedCard[position].PersonThought + " Person" + "</b></font> <font color=#333333> thought</font>";
            vh.PersonThought.SetText(Html.FromHtml(personThoughtText), TextView.BufferType.Spannable);
        }
        else
        {
            vh.PersonThought.Text = "0 Person thought";
        }

        if (mNewsfeedCard[position].AlreadyLiked)
            vh.LikeUnlikeCheckBox.Checked = true;
        else
            vh.LikeUnlikeCheckBox.Checked = false;

        vh.LikeUnlikeCheckBox.CheckedChange += LikeUnlikeCheckBox_CheckedChange;
    }

    private async void LikeUnlikeCheckBox_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
    {
        if (e.IsChecked)
        {
            //Need to pass "FeedID" parameter in API below
            var result = await Api.GetLikesUnlikes(FeedID);
            if (result != null)
            {
                if (result.ResponseMessage == "Thanks succeeded")
                {

                }
            }
        }
    }

    public override int ItemCount
    {
        get { return mNewsfeedCard.NumFeeds; }
    }

    void OnClick(int position)
    {
        ItemClick?.Invoke(this, position);
    }
}

知道如何获取模型的值并传入LikeUnlikeCheckBox_CheckedChange()mehthod?

0 个答案:

没有答案