listview item中包含edittext(手动可输入)

** item 中包含两个edittext 并且都可以输入
代码如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_secondary_setting_layout, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    fillData(position, holder);
//        convertView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
                Toast.makeText(mContext, "点击了item", Toast.LENGTH_LONG).show();
//                return true;
//            }
//        });
    return convertView;
}

private void fillData(final int position, final ViewHolder holder) {
    final ProjectCheckBean.DatasEntity bean = mList.get(position);
    holder.ref = position;

    holder.tvName.setText(bean.getName());
    holder.tvCode.setText(bean.getCode() + "");

    holder.imgAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.AddViewItemClicked(1, bean);
            }
        }
    });
    if (bean.getTagNum() == 1) {
        holder.llOperate.setVisibility(View.VISIBLE);
        holder.cbxSubjectCount.setImageResource(R.drawable.checked);
    } else {
        holder.llOperate.setVisibility(View.GONE);
        holder.cbxSubjectCount.setImageResource(R.drawable.unchecked);
    }
    if (bean.isBottomLevel()) {
        holder.cbxSubjectCount.setVisibility(View.VISIBLE);
    } else {
        holder.cbxSubjectCount.setVisibility(View.INVISIBLE);
    }
//        holder.cbxSubjectCount.setImageResource(R.drawable.unchecked);
    holder.cbxSubjectCount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (bean.getTagNum() == 1) {
                bean.setTagNum(0);
                holder.cbxSubjectCount.setImageResource(R.drawable.unchecked);
            } else {
                bean.setTagNum(1);
                holder.cbxSubjectCount.setImageResource(R.drawable.checked);
            }
            notifyDataSetChanged();
        }
    });

    holder.etNum.addTextChangedListener(new MyTextWatcher(holder, "1"));
    holder.etPrice.addTextChangedListener(new MyTextWatcher(holder, "2"));
    holder.etNum.setText(bean.getNum() + "");
    holder.etPrice.setText(bean.getPrice() + "");
    holder.tvMoney.setText(FontUtils.setMoneyFormat("" + (bean.getNum() * bean.getPrice())));

    // 焦点冲突解决
    holder.etNum.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ((ViewGroup) v.getParent())
                    .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            return false;
        }
    });
    holder.etPrice.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ((ViewGroup) v.getParent())
                    .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            return false;
        }
    });

}

public interface OnAddViewItemClickListener {
    void AddViewItemClicked(int id, ProjectCheckBean.DatasEntity bean);
}

private OnAddViewItemClickListener mListener;

public void setOnAddViewItemClickListener(OnAddViewItemClickListener listener) {
    mListener = listener;
}

public class MyTextWatcher implements TextWatcher {
    private ViewHolder holder;
    private String tag;
    public MyTextWatcher(ViewHolder holder, String tag) {
        this.holder = holder;
        this.tag = tag;
    }

    @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 s) {
        switch (tag) {
            case "1":
                if (!TextUtils.isEmpty(s.toString())) {
                    mList.get(holder.ref).setNum(Double.parseDouble(s.toString()));
                    holder.tvMoney.setText("" + (mList.get(holder.ref).getPrice() * mList.get(holder.ref).getNum()));
                    boolean isRefresh = false;
                    if ((mList.get(holder.ref).getPrice() * mList.get(holder.ref).getNum()) != mList.get(holder.ref).getSumMoney()){
                        isRefresh = true;
                    }
                    mList.get(holder.ref).setSumMoney(Double.parseDouble(holder.tvMoney.getText().toString().trim()));
                    if ( isRefresh) {
                        notifyDataSetChanged();
                    }
                }

                break;
            case "2":
                if (!TextUtils.isEmpty(s.toString())) {
                    mList.get(holder.ref).setPrice(Double.valueOf(s.toString()));
                    holder.tvMoney.setText("" + (mList.get(holder.ref).getPrice() * mList.get(holder.ref).getNum()));
//                        mList.get(holder.ref).setSumMoney(Double.parseDouble(holder.tvMoney.getText().toString().trim()));
//                        if ( mList.get(holder.ref).getSumMoney() > 0) {
//                            notifyDataSetChanged();
//                        }
                    boolean isRefresh = false;
                    if ((mList.get(holder.ref).getPrice() * mList.get(holder.ref).getNum()) != mList.get(holder.ref).getSumMoney()){
                        isRefresh = true;
                    }
                    mList.get(holder.ref).setSumMoney(Double.parseDouble(holder.tvMoney.getText().toString().trim()));
                    if ( isRefresh) {
                        notifyDataSetChanged();
                    }
                }
                break;
            default:
                break;
        }
    }
}

static class ViewHolder {
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.img_add)
    ImageView imgAdd;
    @BindView(R.id.et_price)
    EditText etPrice;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.ll_operate)
    LinearLayout llOperate;
    @BindView(R.id.cbx_subject_count)
    ImageView cbxSubjectCount;
    //
    private int ref;

    ViewHolder(View view) {
        ButterKnife.bind(this, view);
    }
}

布局代码:

item 根布局加上:
android:descendantFocusability=”beforeDescendants”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值