Listview適配器中添加監聽,如何拿到Item中的view

有個需求,通過設置Edittext監聽,當Edittex內容改變時來動態改變相應item裏面其他控件的狀態

直接來看代碼:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHodler holder;
        if (null == convertView) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_custom_receive_goods, parent, false);
            holder = new ViewHodler();
            holder.et_unique_num = convertView.findViewById(R.id.et_unique_num);
            holder.item_iv_scan = convertView.findViewById(R.id.iv_scan);
            holder.iv_clear_no = convertView.findViewById(R.id.iv_clear_no);
            holder.mMyTextWatcher = new EditTextWatcher(position, mStringSparseArray,holder);//通過傳遞holder
            holder.et_unique_num.addTextChangedListener(holder.mMyTextWatcher);
            convertView.setTag(holder);

        } else {
            holder = (ViewHodler) convertView.getTag();
            holder.updatePosition(position);
        }
        holder.iv_clear_no.setTag(position);
        holder.et_unique_num.setText(mStringSparseArray.get(position));
//        if (StringUtil.isNotEmpty(holder.et_unique_num.getText().toString())){
//            holder.iv_clear_no.setVisibility(View.VISIBLE);
//        }else {
//            holder.iv_clear_no.setVisibility(View.GONE);
//        }
//        holder.iv_clear_no.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                holder.et_unique_num.setText("");
//            }
//        });
        holder.et_unique_num.setSelection(holder.et_unique_num.getText().length());
   

        return convertView;
    }

    public class ViewHodler {
        ImageView iv_clear_no;
        ImageView item_iv_scan;
        EditText et_unique_num;
        int position;
        EditTextWatcher mMyTextWatcher;

        public void updatePosition(int position) {
            mMyTextWatcher.updatePosition(position);
        }
    }

    class EditTextWatcher implements TextWatcher {

        public EditTextWatcher(int position, SparseArray<String> sparseArray,ViewHodler viewHodler) {
            this.position = position;
            this.sparseArray = sparseArray;
            this.viewHodler = viewHodler;

        }

        private int position;
        private SparseArray<String> sparseArray;
        private ViewHodler viewHodler;

        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(final Editable editable) {
            if (StringUtil.isNotEmpty(editable.toString())) {
                sparseArray.put(position, editable.toString());
//                viewHodler.iv_clear_no.setVisibility(View.VISIBLE);//去除holder裏面的view
            }else {
//                viewHodler.iv_clear_no.setVisibility(View.GONE);
//                sparseArray.put(position, null);
                sparseArray.delete(position);

            }


        }
    }

通過設置holder,通過holder拿到相應控件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章