RecyclerView 多條目型別含有多個EditText

baidu_24743861發表於2020-10-15

 

 

public class PositionSalaryModifyAdapter extends BaseMultiItemQuickAdapter<SalaryBean.RowsBean, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public PositionSalaryModifyAdapter(List<SalaryBean.RowsBean> data) {
        super(data);
        addItemType(1, R.layout.item_position_salary_modify_type1);
        addItemType(2, R.layout.item_position_salary_modify_type2);

    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, SalaryBean.RowsBean bean) {
        switch (holder.getItemViewType()) {
            case 1:
                holder.setText(R.id.tv_department_name, bean.getDepName());
                holder.setText(R.id.edt_zjl_salary, setMoney(bean.getGeneral()));
                holder.setText(R.id.edt_fzjl_salary, setMoney(bean.getVice()));
                int position = holder.getLayoutPosition();
                EditText edtZjlSalary = holder.getView(R.id.edt_zjl_salary);
                EditText edtFzjlSalary = holder.getView(R.id.edt_fzjl_salary);
                editSetOnFocusChangeListener(edtZjlSalary, position, 0);
                editSetOnFocusChangeListener(edtFzjlSalary, position, 1);
                break;
            case 2:
                holder.setText(R.id.tv_department_name, bean.getDepName());
                holder.setText(R.id.edt_bmjl_salary, setMoney(bean.getDepartment()));
                holder.setText(R.id.edt_cbjl_salary, setMoney(bean.getTrainee()));
                holder.setText(R.id.edt_bmzg_salary, setMoney(bean.getCharge()));
                holder.setText(R.id.edt_cbzg_salary, setMoney(bean.getReserve()));
                holder.setText(R.id.edt_yg_salary, setMoney(bean.getStaff()));

                int positionTwo = holder.getLayoutPosition();
                EditText edtBmjlSalary = holder.getView(R.id.edt_bmjl_salary);
                EditText edtCbjlSalary = holder.getView(R.id.edt_cbjl_salary);
                EditText edtBmzgSalary = holder.getView(R.id.edt_bmzg_salary);
                EditText edtCbzgSalary = holder.getView(R.id.edt_cbzg_salary);
                EditText edtYgSalary = holder.getView(R.id.edt_yg_salary);
                editSetOnFocusChangeListener(edtBmjlSalary, positionTwo, 2);
                editSetOnFocusChangeListener(edtCbjlSalary, positionTwo, 3);
                editSetOnFocusChangeListener(edtBmzgSalary, positionTwo, 4);
                editSetOnFocusChangeListener(edtCbzgSalary, positionTwo, 5);
                editSetOnFocusChangeListener(edtYgSalary, positionTwo, 6);

                break;
        }
    }

    private String setMoney(double money) {
        DecimalFormat df = new DecimalFormat("######0.00");
        if (money == 0.0 || money == 0.00 || money == 0) {
            return "";
        }
        return df.format(money);

    }

    //以下程式碼為重點
    private void editSetOnFocusChangeListener(EditText edt, int position, int i) {
        TextWatcher textWatcher = 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) {
                //刪除.後面超過兩位的數字
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        edt.setText(s);
                        edt.setSelection(s.length());
                    }
                }

                //如果.在起始位置,則起始位置自動補0
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    edt.setText(s);
                    edt.setSelection(2);
                }

                //如果起始位置為0並且第二位跟的不是".",則無法後續輸入
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        edt.setText(s.subSequence(0, 1));
                        edt.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

                switch (i) {
                    case 0:
                        if (s.length() > 0) {
                            getData().get(position).setGeneral(Double.parseDouble(s.toString()));
                        } else {
                            getData().get(position).setGeneral(0.00);
                        }
                        break;
                    case 1:
                        if (s.length() > 0) {
                            getData().get(position).setVice(Double.parseDouble(s.toString()));
                        } else {
                            getData().get(position).setVice(0.00);
                        }
                        break;
                    case 2:
                        if (s.length() > 0) {
                            getData().get(position).setDepartment(Double.parseDouble(s.toString()));
                        } else {
                            getData().get(position).setDepartment(0.00);
                        }
                        break;
                    case 3:
                        if (s.length() > 0) {
                            getData().get(position).setTrainee(Double.parseDouble(s.toString()));
                        } else {
                            getData().get(position).setTrainee(0.00);
                        }
                        break;

                    case 4:
                        if (s.length() > 0) {
                            getData().get(position).setCharge(Double.parseDouble(s.toString()));
                        } else {
                            getData().get(position).setCharge(0.00);
                        }
                        break;
                    case 5:
                        if (s.length() > 0) {
                            getData().get(position).setReserve(Double.parseDouble(s.toString()));
                        } else {
                            getData().get(position).setReserve(0.00);
                        }
                        break;

                    case 6:
                        if (s.length() > 0) {
                            getData().get(position).setStaff(Double.parseDouble(s.toString()));
                        } else {
                            getData().get(position).setStaff(0.00);
                        }
                        break;
                }
            }
        };

        edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    edt.addTextChangedListener(textWatcher);
                } else {
                    edt.removeTextChangedListener(textWatcher);
                }
            }
        });
    }

}

 

相關文章