2017-10-19 121 views
0

我想在點擊回收站項目的回收站視圖中顯示和隱藏一些按鈕。例如,我有一個像這樣的兩個項目的recyclerw視圖enter image description hererecyclerview內的隱藏/顯示按鈕

單擊第一個項目時,應顯示編輯和刪除按鈕。我的適配器代碼是在這裏:

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

我添加了一個「項目單擊」事件應顯示/隱藏的按鈕。我還想要編輯和刪除按鈕的點擊事件。我怎樣才能做到這一點?任何幫助,將不勝感激。

+0

這個答案,而不是嚴格爲您詳細的問題的答案,並告訴你如何處理使用'RecyclerView'時,單擊事件。 https://stackoverflow.com/questions/44520390/disable-all-other-toggle-buttons-in-recycler-view-after-one-is-clicked/44521692#44521692 – Kuffs

+0

其實,重新讀你的問題和答案,它似乎是一個完美契合。 – Kuffs

+0

我想要一個項目點擊事件來顯示並隱藏該項目內的按鈕 – Arti

回答

1

設置一個適配器變量:

private int currentSelectedPosition = RecyclerView.NO_POSITION 

更改personCardView適配器佈局,在他們這兩個按鈕,並設置其知名度GONE。參考他們在你ViewHolder(如按鈕editButtonButtondeleteButton

在您的項目onClickListener,設置currentPosition並調用notifyDataSetChanged() - 這是需要重新隱藏在先前的選擇按鈕,並顯示在此選擇的按鈕。然後在onBindViewHolder中,應用VISIBLEGONE邏輯如下。我個人設置裏面itemClickListeneronBindViewHolder,所以整個方法是這樣的:

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) 
    } 
+0

我更新它包括顯示如何處理編輯和刪除按鈕的點擊事件,如您所問:-) –

+0

我該如何ViewHolder中的參考按鈕? – Arti

+1

就像您爲2個textview所做的那樣....所以將它們添加到您的PersonCardView佈局文件中,然後使用ID: editButton = itemView.findViewById(R.id.btn_edit); delete = itemView.findViewById(R.id.btn_delete); –