在recyclerview中隐藏/显示按钮

时间:2017-10-19 10:36:15

标签: android android-recyclerview xamarin.android

我想在点击回收器项目的回收器视图中显示和隐藏一些按钮。例如,我有一个带有两个项目的recyclerw视图,例如enter image description here

点击第1项后,应显示编辑和删除按钮。我的适配器代码在这里:

using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;

namespace RecyclerViewTest
{
    internal class BuyerAdapter : RecyclerView.Adapter
    {
        public List<Buyer> mlivestock;
        public event EventHandler<int> ItemClick;

        public BuyerAdapter(List<Buyer> photoAlbum)
        {
            mlivestock = photoAlbum;
        }
        public override RecyclerView.ViewHolder
            OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View itemView = LayoutInflater.From(parent.Context).
                        Inflate(Resource.Layout.PersonCardView, parent, false);
            PhotoViewHolder vh = new PhotoViewHolder(itemView, ItemOnClick);
            return vh;
        }

        void ItemOnClick(int position)
        {
            if (ItemClick != null)
                ItemClick(this, position);
        }

        public override void
            OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            PhotoViewHolder vh = holder as PhotoViewHolder;
            vh.Agent.Text = mlivestock[position].Agent;
            vh.Pen.Text = mlivestock[position].Pen.ToString();

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

    }

    public class PhotoViewHolder : RecyclerView.ViewHolder
    {
        public TextView Agent { get; private set; }
        public TextView Pen { get; private set; }

        public PhotoViewHolder(View itemView, Action<int> listener)
            : base(itemView)
        {
            Agent = itemView.FindViewById<TextView>(Resource.Id.agent);
            Pen = itemView.FindViewById<TextView>(Resource.Id.pen);

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

我添加了一个“ItemClick”事件,该事件应显示/隐藏按钮。我还想要点击事件来编辑和删除按钮。我怎样才能做到这一点?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

设置适配器变量:

private int currentSelectedPosition = RecyclerView.NO_POSITION

更改personCardView适配器布局,使其中包含两个按钮,并将其可见性设置为GONE。在ViewHolder中引用它们(例如按钮editButtonButton deleteButton

在您的项目onClickListener中,设置currentPosition并调用notifyDataSetChanged() - 这是重新隐藏之前选择中的按钮并显示此选项中的按钮所必需的。然后在onBindViewHolder中,应用VISIBLEGONE逻辑,如下所示。我个人也将itemClickListener设置在onBindViewHolder里面,所以整个方法看起来像这样:

public void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)  {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               currentSelectedPosition = position;
               notifyDataSetChanged();
            }
        });
        if (currentSelectedPosition == position) {
             holder.editButton.setVisibility(View.VISIBLE);
             holder.editButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                       // your edit button click event here
                  }
             });
             holder.deleteButton.setVisibility(View.VISIBLE);
             holder.deleteButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                     // your delete button click event here
                  }
             });
        } else {
             holder.editButton.setVisibility(View.GONE);
             holder.deleteButton.setVisibility(View.GONE);
        }
      //..... the rest of your code for onBindViewHolder (updating your text views and so on)
    }