在列表视图Android-相邻的按钮自动点击视图、按钮、列表、Android

2023-09-06 07:52:49 作者:季末℡等你的牵手

我的工作是填充车一个模块上。我用的ListView 和扩展 BaseAdapter 填充车项目。 随着的ListView 每个项目,我嵌入式两粒扣(INC和DEC)来增加和项目在购物车递减量。

I am working on a module that populates cart. I used ListView and extended BaseAdapter to populate the cart items. With each of the item in ListView, I embedded two button(inc and dec) to increment and decrement quantity of item in cart.

的ListView 正确更新,但在快速点击/攻节目突然的行为。递增/递减按钮

ListView is correctly updated, but increment/decrement button on quick click/tapping shows abrupt behaviour.

每当我赶紧点击任INC或DEC键,对应的增量或项目的减速按钮旁边的ListView当前项目自动点击(与当前项目BTN)。

Whenever I quickly tap any of inc or dec button, the corresponding inc or dec button of an item next to current item in ListView is automatically clicked(along with current item btn).

在换句话说,只要我赶紧挖掘的ListView 第i个项目的增量BTN,公司在的ListView 自动点击(与第i个项目的增量BTN)。

In other words, whenever I quickly tap inc btn of ith item in ListView, inc btn of i+1 th item in ListView is automatically clicked( along with inc btn of ith item).

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_cart, parent, false);
        holder = new ViewHolder();
        holder.baseItem = (TextView) convertView.findViewById(R.id.qnt_tv);
        holder.qntInc = (TextView) convertView.findViewById(R.id.inc_btn);
        holder.qntDec = (TextView) convertView.findViewById(R.id.dec_btn);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final CartModel cm = mCart.get(position);
    holder.baseItem.setText(cm.getmTitle());
    holder.qntSel.setText(String.valueOf(cm.getmQnt()));
    holder.qntInc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (qntSpinnerCb != null)
                qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), INCREASE_QNT);
        }
    });
    holder.qntDec.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (qntSpinnerCb != null) {
                qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), DECREASE_QNT);
            }
        }
    });

    return convertView;
}

有关回调接口

public interface CartQntSpinnerListenerCallBack {
    void changeQuantityOfSelectedItemInCart(String iId, char changeType);
}

试过调试,摸不清这个怪异的行为。

Tried debugging, unable to figure out this weird behavior.

推荐答案

您没有选择正确的 CartModel 里面的onClick,如果你想获得正确的对象里面的onclick那么你有标记的位置按钮

You are not selecting correct CartModel inside onClick, if you want to get correct object inside onclick then you have to tag position to button

holder.qntInc.setTag(position);

在的onClick:

@Override
        public void onClick(View view) {
            if (qntSpinnerCb != null){
                CartModel cm= mCart.get((Integer)view.getTag);
                qntSpinnerCb.changeQuantityOfSelectedItemInCart(cm.getmIid(), INCREASE_QNT);
            }
        }

按照相同的qntDec。

Follow the same for qntDec.