How to handle button click inside recycler view item in Xamarin Android?

HarshitaHarshita INMember ✭✭✭✭
edited September 10 in Xamarin.Android

here is the item inside recycler view,so I want to increase and decrease the quantity by clicking plus and minus button.How to do this in xamarin android?

here is my adapter

public class OrderDetailsItemAdapter : RecyclerView.Adapter
      {
    List<OrderItem> orderItemList = new List<OrderItem>();
    RecyclerView orderItemsRecyclerView;
    private object orderListitems;
    private RecyclerView recyclerview;
    Context _context;

    public OrderDetailsItemAdapter(List<OrderItem> orderItemList, RecyclerView recyclerview,Context context)
    {
        this.orderItemList = orderItemList;
        this.recyclerview = recyclerview;
        _context = context;
    }

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

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

        viewHolder.OrderItemCheckBox.Checked = orderItemList[position].IsChecked;
        viewHolder.OrderItemSKU.Text = $"SKU: {orderItemList[position].SKU}";
        viewHolder.KitIcon.Visibility = orderItemList[position].IsKit ? ViewStates.Visible : ViewStates.Invisible;
        viewHolder.OrderItemKitSKU.Text = $"KIT SKU: {orderItemList[position].KitSKU}";
        viewHolder.OrderItemQty.Text = $"REQ QTY: {orderItemList[position].OrderQuantity.ToString()}";

        viewHolder.OrderItemShipQty.Text = orderItemList[position].ShipQuantity.ToString();
    }


    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        LayoutInflater inflater = LayoutInflater.From(parent.Context);
        View itemView = inflater.Inflate(Resource.Layout.order_details_layout_view, parent, false);
        return new OrderDetailsItemViewHolder(itemView);
    }
}

so how to get the position of the item on button click?

Best Answer

Answers

  • HarshitaHarshita INMember ✭✭✭✭

    @LeonLu
    Thanks a lot. :)

  • vineethavijayanvineethavijayan USMember ✭✭

    @LeonLu said:
    You can achieve it in the OnBindViewHolder. Here is running GIF.

    And here is code.

      public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
            myHolder.Add_Button.Click +=(o,e)=> {
                mEmails[position].MyCount = mEmails[position].MyCount + 1;
                myHolder.editText1.Text = mEmails[position].MyCount.ToString();
    
            };
            myHolder.reduce_Button.Click += (o, e) =>
            {
                mEmails[position].MyCount = mEmails[position].MyCount - 1;
                myHolder.editText1.Text = mEmails[position].MyCount.ToString();
            };
    
            myHolder.mName.Text = mEmails[position].Name;
      }
    

    Here is my demo.

    But when we are scrolling the recyclerview the edittextvalue position is swapping.

  • vineethavijayanvineethavijayan USMember ✭✭

    But when we are scrolling the recyclerview the edittextvalue position is swapping.

Sign In or Register to comment.