如何在Xamarin Android中处理回收器视图项目中的按钮点击?

epfja78i  于 2023-06-20  发布在  Android
关注(0)|答案(3)|浏览(141)

这里是回收器视图内的项目,所以我想通过点击加号和减号按钮来增加和减少数量.如何在xamarin Android中做到这一点?
这是我的适配器

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

那么如何获得按钮点击时项目的位置呢?

t40tm48m

t40tm48m1#

可以在OnBindViewHolder方法中处理逻辑

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

krcsximq2#

你可以使用ElegantButton控件,这是一个专门用于此目的的自定义控件

public class ElegantNumberButton : RelativeLayout
{
    private Context context;
    private IAttributeSet attrs;
    private int styleAttr;
    private IOnClickListener mListener;
    private int initialNumber;
    private int lastNumber;
    private int currentNumber;
    private int finalNumber;
    private TextView textView;
    private Button addBtn, subtractBtn;
    private View view;
    private IOnValueChangeListener mOnValueChangeListener;

    public ElegantNumberButton(Context context) : base(context)
    {
        this.context = context;
        InitView();
    }

    public ElegantNumberButton(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        this.context = context;
        this.attrs = attrs;
        InitView();
    }

    public ElegantNumberButton(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
        this.context = context;
        this.attrs = attrs;
        this.styleAttr = defStyleAttr;
        InitView();
    }

    private void InitView()
    {
        this.view = this;
        Inflate(context, Resource.Layout.layout, this);
        Resources res = this.Resources;
        int defaultColor = ResourcesCompat.GetColor(res, Resource.Color.colorPrimary, null);
        int defaultTextColor = ResourcesCompat.GetColor(res, Resource.Color.colorPrimary, null);
        Drawable defaultDrawable = res.GetDrawable(Resource.Drawable.background);

        TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.ElegantNumberButton, styleAttr, 0);

        initialNumber = a.GetInt(Resource.Styleable.ElegantNumberButton_initialNumber, 0);
        finalNumber = a.GetInt(Resource.Styleable.ElegantNumberButton_finalNumber, int.MaxValue);
        float textSize = a.GetDimension(Resource.Styleable.ElegantNumberButton_textSize, 13);
        var color = a.GetColor(Resource.Styleable.ElegantNumberButton_backGroundColor, defaultColor);
        var textColor = a.GetColor(Resource.Styleable.ElegantNumberButton_textColor, defaultTextColor);
        Drawable drawable = a.GetDrawable(Resource.Styleable.ElegantNumberButton_backgroundDrawable);

        subtractBtn = (Button)FindViewById(Resource.Id.subtract_btn);
        addBtn = (Button)FindViewById(Resource.Id.add_btn);
        textView = (TextView)FindViewById(Resource.Id.number_counter);
        LinearLayout mLayout = (LinearLayout)FindViewById(Resource.Id.layout);

        subtractBtn.SetTextColor(textColor);
        addBtn.SetTextColor(textColor);
        textView.SetTextColor(textColor);
        subtractBtn.SetTextSize(ComplexUnitType.Sp, textSize);
        addBtn.SetTextSize(ComplexUnitType.Sp, textSize);
        textView.SetTextSize(ComplexUnitType.Sp, textSize);

        if (drawable == null)
        {
            drawable = defaultDrawable;
        }

        drawable.SetColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.Src));
        if (Build.VERSION.SdkInt > BuildVersionCodes.JellyBean)
            mLayout.Background = (drawable);
        else

            mLayout.SetBackgroundDrawable(drawable);

        textView.SetText(initialNumber);

        currentNumber = initialNumber;
        lastNumber = initialNumber;
        subtractBtn.Click += SubtractBtn_Click;
        addBtn.Click += AddBtn_Click;
        a.Recycle();
    }
    private void SubtractBtn_Click(object sender, EventArgs e)
    {
        int num = int.Parse(textView.Text);
        SetNumber((num - 1).ToString(), true);
    }

    private void AddBtn_Click(object sender, EventArgs e)
    {
        int num = int.Parse(textView.Text.ToString());
        SetNumber((num + 1).ToString(), true);
    }

    private void CallListener(View view)
    {
        if (mListener != null)
        {
            mListener.OnClick(view);
        }

        if (mOnValueChangeListener != null)
        {
            if (lastNumber != currentNumber)
            {
                mOnValueChangeListener.OnValueChange(this, lastNumber, currentNumber);
            }
        }
    }

    public string GetNumber()
    {
        return (currentNumber).ToString();
    }

    public void SetNumber(string number)
    {
        lastNumber = currentNumber;
        this.currentNumber = int.Parse(number);
        if (this.currentNumber > finalNumber)
        {
            this.currentNumber = finalNumber;
        }
        if (this.currentNumber < initialNumber)
        {
            this.currentNumber = initialNumber;
        }
        textView.Text = currentNumber.ToString();
    }

    public void SetNumber(string number, bool notifyListener)
    {
        SetNumber(number);
        if (notifyListener)
        {
            CallListener(this);
        }
    }

    public void SetOnClickListener(IOnClickListener onClickListener)
    {
        this.mListener = onClickListener;
    }

    public void SetOnValueChangeListener(IOnValueChangeListener onValueChangeListener)
    {
        mOnValueChangeListener = onValueChangeListener;
    }

    public interface IOnClickListener
    {
        void OnClick(View view);
    }

    public interface IOnValueChangeListener
    {
        void OnValueChange(ElegantNumberButton view, int oldValue, int newValue);
    }

    public void SetRange(int startingNumber, int endingNumber)
    {
        this.initialNumber = startingNumber;
        this.finalNumber = endingNumber;
    }

    public void UpdateColors(Android.Graphics.Color backgroundColor, Android.Graphics.Color textColor)
    {
        this.textView.SetBackgroundColor(backgroundColor);
        this.addBtn.SetBackgroundColor(backgroundColor);
        this.subtractBtn.SetBackgroundColor(backgroundColor);

        this.textView.SetTextColor(textColor);
        this.addBtn.SetTextColor(textColor);
        this.subtractBtn.SetTextColor(textColor);
    }

    public void UpdateTextSize(float newSize)
    {
        this.textView.SetTextSize(ComplexUnitType.Sp, newSize);
        this.addBtn.SetTextSize(ComplexUnitType.Sp, newSize);
        this.subtractBtn.SetTextSize(ComplexUnitType.Sp, newSize);
    }
}

你需要的所有资源都可以在下面的git中找到:
https://github.com/ashik94vc/ElegantNumberButton/tree/master/lib/src/main
一旦你在xml中使用了它,你就可以使用GetNumber方法从对象中获取数据。

epggiuax

epggiuax3#

我知道这是一个老帖子,但在任何情况下,我想帮助你管理你的适配器上的绑定函数的Click事件,但为了防止多次调用事件,我建议使用一个属性从按钮,告诉你它是否有或没有ClickEvent注册以前:

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

      if(!viewHolder.MyButton.HasOnClickListener){
          viewHolder.MyButton.Click += (s, e) =>{
            //TO DO
            }
        }
    }

相关问题