Проблема с RecyclerView некорректное удаление и добавление в список элементов

Есть список элементов RecyclerView.

На каждом элементе есть кнопка удалить его, с идеей, что например при удалении 3-го участка, на 4-ом просто изменяется шапка, данные в полях неизменны.

Что происходит при нажатии кнопки удалить на участке 3

Появляется просто пустой элемент. А если еще раз добавить то уже 4-ый.

введите сюда описание изображения

Код ListAdapter

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private final LayoutInflater inflater;
List<Section> sections;
private Section section;
public TypeCalculation typeCalculation;
public static boolean maxSectionsCount = false;
public static boolean wasItemRemoved = false;


interface OnDeleteClickListener {
    void onDeleteClick(Section section, int position);
}

public ListAdapter(Context context, List<Section> sections, TypeCalculation typeCalculation) {
    this.inflater = LayoutInflater.from(context);
    this.sections = sections;
    this.typeCalculation = typeCalculation;
}


@Override
public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.section, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ListAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
    section = sections.get(position);
    holder.nameSection.setText("Участок #" + (position + 2));
    if (holder.flowSection.getLayoutParams().height != 0)
        sections.get(position).setUniqueFlow(true);
    else sections.get(position).setUniqueFlow(false);
    if (typeCalculation != TypeCalculation.FindFlowPress) {
        holder.checkBoxSection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    section.setUniqueFlow(true);
                    holder.flowSection.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                    holder.flowSectionText.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                    holder.flowSection.setText("");
                    holder.flowSection.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                            try {
                                final String regex = "^([1-9]*[1-9][0-9]*(\\.[0-9]+)?|[0]+\\.[0-9]*[1-9][0-9]*)";
                                final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
                                final Matcher matcher = pattern.matcher(holder.flowSection.getText().toString());
                                if (!v.hasFocus() && !matcher.matches()) {
                                    //Всплывающая подсказка при неверном вводе исходных данных
                                    Snackbar snackbar = Snackbar
                                            .make(v, "Внимание: Значение поля \"Расход на участке #\"" + (position + 2) + " не введено или равно нулю", 5000);
                                    View snackbarView = snackbar.getView();
                                    TextView textView = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
                                    textView.setMaxLines(5);
                                    snackbar.show();
                                }
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    section.setUniqueFlow(false);
                    holder.flowSection.getLayoutParams().height = 0;
                    holder.flowSectionText.getLayoutParams().height = 0;
                    holder.flowSection.setOnFocusChangeListener(null);
                    holder.flowSection.setText("0");
                }
                holder.flowSectionText.requestLayout();
                holder.flowSection.requestLayout();
            }
        });
    } else {
       /* holder.hEndSection.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
        holder.hEndSectionText.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;*/
        holder.checkBoxSection.getLayoutParams().height = 0;
        holder.checkBoxSection.setChecked(false);
    }
    
    holder.deleteButton.setOnClickListener(new View.OnClickListener() {
        @SuppressLint({"ResourceAsColor", "ClickableViewAccessibility", "NotifyDataSetChanged"})
        @Override
        public void onClick(View v) {
            ListAdapter.maxSectionsCount = false;
            int actualPosition = holder.getAdapterPosition();


            sections.remove(actualPosition);
            notifyItemRemoved(actualPosition);
            notifyItemRangeChanged(actualPosition, 1);

            if (sections.size() == 0) {
                FloatingActionButton fab = null;
                if (typeCalculation == TypeCalculation.FindHend) {
                    fab = PressFindHend.fab;
                    fab.setY(PressFindHend.textFAB.getY() + (int) (PressFindHend.textFAB.getHeight() / 2) - (int) (fab.getHeight() / 2));
                    fab.setX(0);
                    fab.setAlpha(1f);
                    fab.setOnTouchListener(null);
                    PressFindHend.textFAB.setVisibility(View.VISIBLE);
                    PressFindHend.isMultiSections = false;
                    PressFindHend.separator.setVisibility(View.INVISIBLE);
                    PressFindHend.checkBoxWaterHammer.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    PressFindHend.checkBoxWaterHammer.requestLayout();
                } else if (typeCalculation == TypeCalculation.FindHstart) {
                    fab = PressFindHstart.fab;
                    fab.setY(PressFindHstart.textFAB.getY() + (int) (PressFindHstart.textFAB.getHeight() / 2) - (int) (fab.getHeight() / 2));
                    fab.setX(0);
                    fab.setAlpha(1f);
                    fab.setOnTouchListener(null);
                    PressFindHstart.textFAB.setVisibility(View.VISIBLE);
                    PressFindHstart.separator.setVisibility(View.INVISIBLE);
                    PressFindHstart.checkBoxWaterHammer.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    PressFindHstart.checkBoxWaterHammer.requestLayout();
                    PressFindHend.isMultiSections = false;
                } else if (typeCalculation == TypeCalculation.FindFlowPress) {
                    fab = PressFindPressFlow.fab;
                    fab.setY(PressFindPressFlow.textFAB.getY() + (int) (PressFindPressFlow.textFAB.getHeight() / 2) - (int) (fab.getHeight() / 2));
                    fab.setX(0);
                    fab.setAlpha(1f);
                    fab.setOnTouchListener(null);
                    PressFindPressFlow.textFAB.setVisibility(View.VISIBLE);
                    PressFindPressFlow.separator.setVisibility(View.INVISIBLE);
                    PressFindPressFlow.checkBoxWaterHammer.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    PressFindPressFlow.checkBoxWaterHammer.requestLayout();
                    PressFindHend.isMultiSections = false;


                }
                ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) fab.getLayoutParams();
                p.setMargins(0, 0, 0, 0);
            }
        }
    });
    holder.flowSection.addTextChangedListener(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.equals("")) sections.get(position).setFlow(0);
            else if (s.length() != 0 && !s.equals("0"))
                sections.get(position).setFlow(Double.parseDouble(s.toString()));
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.equals("")) sections.get(position).setFlow(0);
            else if (s.length() != 0 && !s.equals("0"))
                sections.get(position).setFlow(Double.parseDouble(s.toString()));
        }
    });
    holder.lengthSection.addTextChangedListener(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.equals("")) sections.get(position).setLength(0);
            else if (s.length() != 0 && !s.equals("0"))
                sections.get(holder.getAdapterPosition()).setLength(Double.parseDouble(s.toString()));
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
    holder.zStartSection.addTextChangedListener(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.equals("")) sections.get(position).setzStart(0);
            else if (s.length() != 0 && !s.equals("0"))
                sections.get(position).setzStart(Double.parseDouble(s.toString()));
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    holder.hEndSection.addTextChangedListener(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.equals("")) sections.get(position).sethEnd(0);
            else if (s.length() != 0 && !s.equals("0"))
                sections.get(position).sethEnd(Double.parseDouble(s.toString()));
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}


@Override
public int getItemCount() {
    return sections.size();
}

Ответы (0 шт):