如何动态删除使用 addView() 添加的视图

How to dynamically delete a view added with addView()

我正在制作一个对话框来写评论。

使用addView()动态添加评论字段。

每次按EnterKey时创建一个comment model class并创建一个模型class。

到目前为止,还不错。

但是问题出在删除上

在我的代码中,当我删除一个项目时,只有最后一个项目被删除。

换句话说,只删除了最后一项。

如何动态删除当前焦点项?


CommentModel.java

public class CommentModel {

    public interface EditInputListener {
        void OnEnterPressed();
        void OnDeleteKeyPressed();
    }

    @Nullable
    private CommentModel.EditInputListener listener;
    private EditText commentEdit;

    private final View rootView;

    public CommentModel(@NonNull LayoutInflater layoutInflater, @Nullable ViewGroup parent) {
        rootView = layoutInflater.inflate(R.layout.writing_comment_item, parent, false);
        initView();
    }

    private void initView() {
        commentEdit = this.findViewById(R.id.comment_edit);
        commentEdit.setSingleLine(true);

        // addView Event
        commentEdit.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                if (listener != null) {
                    listener.OnEnterPressed();
                    return true;
                }
            }
            return false;
        });

        // removeView Event
        commentEdit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(commentEdit.getText().toString().length() == 0) {
                    if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
                        listener.OnDeleteKeyPressed();
                        return true;
                    }
                }
                return false;
            }
        });
    }

    public void addListener(CommentModel.EditInputListener listener) {
        this.listener = listener;
    }

    public void setFocus() {
        commentEdit.requestFocus();
    }

    public View getRootView() {
        return rootView;
    }

    private Context getContext() {
        return rootView.getContext();
    }

    private <T extends View> T findViewById(@IdRes int id) {
        return rootView.findViewById(id);
    }
}

WritingCommentDialogFragment.java

public class WritingCommentDialogFragment extends DialogFragment implements CommentModel.EditInputListener {
    LinearLayout mContainer; // input layout
    CommentModel commentItem;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);

        bindViews(view);
        addCommentItem(); // First Item

        return view;
    }

    private void bindViews(View v) {
        mContainer = v.findViewById(R.id.container);
    }

    private void addCommentItem() {
        commentItem = new CommentModel(getLayoutInflater(), null);
        commentItem.addListener(this);
        commentItem.setFocus();
        mContainer.addView(commentItem.getRootView());
    }

    @Override
    public void OnEnterPressed() {
        if (mContainer.getChildCount() >= 5) {
            return;
        }
        addCommentItem();
    }

    @Override
    public void OnDeleteKeyPressed() {
        if (mContainer.getChildCount() <= 1) {
            return;
        }
        mContainer.removeView(commentItem.getRootView());
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    @Override
    public void onResume() {
        super.onResume();
        setDialogSize();
    }

    private void setDialogSize() {
        getDialog().getWindow().setLayout(1000, 1000);
    }
}

改变

mContainer.removeView(commentItem.getRootView());

mContainer.removeView(mContainer.getFocusedChild());

试试这个