我在每个项目上都有一个TextView,并在holder.item_edit_text.addTextChangedListener上添加了onBindViewHolder以获得较长列表,并且该接口在片段级别计算总数,但是当用户在一个视图上键入时,很奇怪,在多个项目上调用onBindViewHolder
IE,如果用户输入5,我将获得5的倍数,具体取决于回收率(列表中的项目数),例如15或25

RECYCLERVIEW onBindViewHolder

holder.item_edit_text.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence amount, int start, int before, int count) {
            calculateTotal.onAmountAdded(current, amount.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    @Override
      public int getItemCount() {
        if (Accounts != null) {
        return Accounts.size();
    }
    return 0;
   }


问题是我如何确保每次在recyclerview中为一项调用onBindViewHolder

片段

     @Override
public void onAmountAdded(Account account, String amount) {
private Map<Account, String> orderItems = new HashMap<>();
    if (amount.length() < 9) {
        orderItems.put(account, amount);
        int newTotal = 0;
        Iterator iterator = orderItems.entrySet().iterator();
        while (iterator.hasNext()) {
            try {
                Map.Entry mentry = (Map.Entry) iterator.next();
                newTotal += Integer.parseInt(mentry.getValue().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        text_total.setText(ValidationUtil.formatNumber(String.valueOf(newTotal)));
        return;
    }
}

最佳答案

我想没有足够的信息。

而不是在viewBinder上添加TextWatcher

将其添加到您的ViewHolder类中

class ViewHolder extends RecyclerView.ViewHolder {

    private TextView item_edit_text;

    ViewHolder(View itemView) {
        super(itemView);
        item_edit_text = itemView.findViewById(R.id.item_edit_text);
        item_edit_text.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence amount, int start, int before, int count) {
                   //not sure why this current variable is for
                  //if its current Account
                   Accounts current = Accounts.get(getLayoutPosition());
                   calculateTotal.onAmountAdded(current, amount.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }

关于java - Android RecyclerView和onBindViewHolder,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52897859/

10-10 08:12