天天看点

BaseRecyclerViewAdapterHelper框架中item的editText设置监听事件,解决数据错乱

@Override
    protected void convert(BaseViewHolder helper, final TestBean item) {

        EditText item_editText = helper.getView(R.id.item_editText);

//        通过tag判断当前editText是否已经设置监听,有监听的话,移除监听再给editText赋值
        if (item_editText.getTag() instanceof TextWatcher){
            item_editText.removeTextChangedListener((TextWatcher) item_editText.getTag());
        }
//        必须在判断tag后给editText赋值,否则会数据错乱
        item_editText.setText(item.getNum()+"");
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.e("Tag", "FirstAdapter afterTextChanged name---" + item.getName() + ",---" + editable);
                if (!TextUtils.isEmpty(editable)) {
                    item.setNum(Integer.valueOf(editable + ""));
                }
            }
        };
//        给item中的editText设置监听
        item_editText.addTextChangedListener(watcher);
//        给editText设置tag,以便于判断当前editText是否已经设置监听
        item_editText.setTag(watcher);

    }
           

继续阅读