首页 > 解决方案 > How to dynamically delete a view added with addView()

问题描述

I am making a dialog to write a comment.

Dynamically add comment fields using addView().

Create a comment model class and create a model class every time you press EnterKey.

So far, it's good.

But the problem lies in deletion.

In my code, when I delete an item, only the last item is deleted.

In other words, only one last item is deleted.

How do I dynamically delete the currently focused item?


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);
    }
}

标签: androidandroid-edittextandroid-dialogfragment

解决方案


Change

mContainer.removeView(commentItem.getRootView());

To

mContainer.removeView(mContainer.getFocusedChild());

Try this


推荐阅读