0

I want to show and hide some buttons within the recycler view in recycler item clicked. For example I have a recyclerw view with two items like this enter image description here

On click of the 1st item, the buttons Edit and delete should be displayed. My adapter code is here:

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);
        }
    }
}

I have added a "ItemClick" event which should show/hide the buttons. I would also want to have click events for edit and delete buttons. How can I achieve this? Any help would be appreciated.

Arti
  • 2,993
  • 11
  • 68
  • 121
  • This answer whilst not strictly an answer for your exact question, does show you how to handle click events when using `RecyclerView`. https://stackoverflow.com/questions/44520390/disable-all-other-toggle-buttons-in-recycler-view-after-one-is-clicked/44521692#44521692 – Kuffs Oct 19 '17 at 10:41
  • Actually, re-reading your question and the answer, it seems a perfect fit. – Kuffs Oct 19 '17 at 10:47
  • I want an item click event to show and hide buttons inside that item – Arti Oct 19 '17 at 11:00
  • If you read and understand the answer I linked to, you will be able to do that. The test adapter code in that answer is very similar to what you want. Adapt it to your needs once you understand it. The only difference currently is that you want to change the Visibility of a button whilst the one I showed you changes the Checked property. – Kuffs Oct 19 '17 at 11:01
  • A complete demo for you: https://github.com/kuffs/RecyclerView-Demo – Kuffs Oct 19 '17 at 11:37
  • Hi @Kuffs your demo is perfect. I want to hide/show a particular view from RecyclerView using a toggle Switch which is on the Toolbar in MainActivity. Since the MainActivity and ViewHolder have different layouts, I am unable to achieve this. How do I achieve it? – Sandeep Yohans May 08 '19 at 07:13
  • 1
    @SandeepYohans This isn't how StackOverflow works. You need to open a new question of your own. Commenting on someone else's question like this will severely limit the number of people who see it. – Kuffs May 08 '19 at 14:24
  • @Kuffs I did it here https://stackoverflow.com/q/56035866/1199154 Please check and reply. Thank you. – Sandeep Yohans May 08 '19 at 15:28

1 Answers1

2

Set an adapter variable:

private int currentSelectedPosition = RecyclerView.NO_POSITION

Change your personCardView adapter layout to have both the buttons in them, and set their visibility to GONE. Reference them in your ViewHolder (e.g. Button editButton, Button deleteButton)

In your item onClickListener, set the currentPosition and call notifyDataSetChanged() - this is necessary to re-hide the buttons in previous selections and show the buttons in this selection. Then in onBindViewHolder, apply the VISIBLE or GONE logic as below. I personally set the itemClickListener inside onBindViewHolder too, so the whole method would look like this:

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)
    }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Vin Norman
  • 2,749
  • 1
  • 22
  • 33
  • I updated it to include showing how to handle the click events for the edit and delete button, as you asked :-) – Vin Norman Oct 19 '17 at 11:35
  • how do I Reference buttons in ViewHolder? – Arti Oct 19 '17 at 11:40
  • 1
    just as you did for your 2 textviews.... so add them to your PersonCardView layout file, with ids, then: editButton = itemView.findViewById(R.id.btn_edit); delete = itemView.findViewById(R.id.btn_delete); – Vin Norman Oct 19 '17 at 11:41
  • Thank you very much for the help. Just one small query, If i have to delete the selected item then how would I have to do it? – Arti Oct 19 '17 at 12:10
  • 1
    assuming your List mLiveStock is a regular ArrayList, you can have some sort of mLiveStock.remove(position) line in the onClick of your delete button, followed by notifyDataSetChanged() ... there's good answers for removing items from recyclerviews though, it's a different question really... here's an answer that looks good: https://stackoverflow.com/questions/26076965/android-recyclerview-addition-removal-of-items – Vin Norman Oct 19 '17 at 12:18