Creating a Delete Button in a RecyclerView Item

njsokalskinjsokalski Member ✭✭✭

I have a RecyclerView in which each item includes a Button that I want to delete the current item (the item that contains the Button). However, because of RecyclerView's recycling feature (at least I think that is the reason) something is getting messed up with the indexes. I am able to handle the Click event with no problem, but am not sure what to do in it to keep all the indexes working and update the RecyclerView. What should I do to create a delete Button in each item? Thanks.

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited January 13

    You do not need to use position, juse add a View.IOnClickListener event for the DeleteButton. When we click the DeleteButton, we can remove the item we clicked and refresh UI.

    For example :

    class MyRemoveItem : Java.Lang.Object, View.IOnClickListener
            {
                Photo photo;
                int position;
                public MyRemoveItem(Photo item,int position) {
                    this.photo = item;
                    this.position = position;
                }
    
                public void OnClick(View v)
                {
                    mPhotoAlbum.Remove(photo);   
                    adapter.NotifyDataSetChanged();
                }
            }
    

    and use like this in method OnBindViewHolder

       public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
            {
                PhotoViewHolder vh = holder as PhotoViewHolder;
                Photo item=   mPhotoAlbum[position];
    
                vh.DeleteButton.SetOnClickListener(new MyRemoveItem(item,position));
            }
    

    PhotoAlbumAdapter.cs (the adapter of my sample)

    public class PhotoAlbumAdapter: RecyclerView.Adapter
        {
            public event EventHandler<int> ItemClick;
            public static List<Photo> mPhotoAlbum = new List<Photo>();
    
            public static RecyclerView.Adapter adapter;
    
            public PhotoAlbumAdapter(List<Photo> branchesList)
            {
                adapter = this;
                mPhotoAlbum = branchesList;
            }
    
            public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
            {
                View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.PhotoCardView, parent, false);
    
                PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick);
    
                return vh;
            }
    
            public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
            {
                PhotoViewHolder vh = holder as PhotoViewHolder;
                //***********************
                Photo item=   mPhotoAlbum[position];
    
                vh.Caption.Text = item.Caption;
    
                vh.MyCheckBox.SetOnCheckedChangeListener(null);
                vh.MyCheckBox.SetOnCheckedChangeListener(new MyListener(item));
                vh.MyCheckBox.Checked = item.isChecked;
    
                vh.DeleteButton.SetOnClickListener(new MyRemoveItem(item,position));
    
            }
    
                class MyRemoveItem : Java.Lang.Object, View.IOnClickListener
                {
                    Photo photo;
                    int position;
                    public MyRemoveItem(Photo item,int position) {
                        this.photo = item;
                        this.position = position;
                    }
    
                    public void OnClick(View v)
                    {       
                        mPhotoAlbum.Remove(photo);
    
                        adapter.NotifyDataSetChanged();
                    }
                }
    
                class MyListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener
                {
                    Photo photo;
    
                    public MyListener( Photo item)
                    {
                        this.photo = item;
                    }
    
                    public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
                    {
                        photo.isChecked = isChecked;
                    }
                }
    
                public override int ItemCount
                {
                    get { return mPhotoAlbum.Count; }
                }
    
                // Raise an event when the item-click takes place:
                void OnClick(int position)
                {
                    if (ItemClick != null)
                        ItemClick(this, position);
                }
            }
    
  • jezhjezh Member, Xamarin Team Xamurai

    @njsokalski Have you resolved your question?

Sign In or Register to comment.