使用 addView() 动态添加自定义布局是不可能的?

问题描述

我正在制作一个窗口来打开 dialog window 并发表评论

当您打开对话窗口时,会出现一个 layout,您可以在其中输入您的第一条评论

评论只能写在一行中,下面的评论布局是通过在输入字段中按EnterKey创建的。

我最初将其实现为 RecyclerView,但现在我也在尝试其他方法。 (使用addView()

但是当我使用 addView() 时出现以下错误

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
@H_502_23@

此问题的解决方显示here 中。 (如果您能解释发生此错误的原因,我将不胜感激。)

使用这个没有错误。 但是,不会添加更多项目。

打开对话框时只有first item存在,按Enter key没有反应。

我想实时动态添加项目。

我该怎么办?


fragment_wc_dialog.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".fragment.WritingCommentDialogFragment">
</LinearLayout>
@H_502_23@

wc_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".fragment.WritingCommentDialogFragment">

    <EditText
        android:id="@+id/comment_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:gravity="center_vertical"
        android:drawableLeft="@drawable/ic_bullet_point"
        android:drawablePadding="5dp"
        android:layout_marginHorizontal="10dp"
        android:background="@null"
        android:textSize="15sp"
        android:inputType="text"
        android:maxLines="1"
        android:maxLength="22"
        android:imeOptions="actionNone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
@H_502_23@

WCDialogFragment.java

public class WritingCommentDialogFragment extends DialogFragment {
    LinearLayout mContainer; // input layout
    View inputView;
    EditText editText;

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

        initViews(view);

        mContainer.addView(inputView); // The first item to be present when the dialog is opened

        editText.setonEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v,int actionId,KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN) {
                    mContainer.addView(inputView);
                }
                return true;
            }
        });

        return view;
    }

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

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getContext(),"onResume()",Toast.LENGTH_SHORT).show();
        setDialogSize();
    }

    private void initViews(View view) {
        mContainer = view.findViewById(R.id.container);
        inputView = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item,null);
        editText = inputView.findViewById(R.id.comment_edit);
    }
}
@H_502_23@

添加

enter image description here

解决方法

据我所知,您的问题和链接的问题不同。该链接有两个视图,他们想要在它们之间来回交换,因此答案是在添加第二个视图之前删除一个视图。您想建立一个视图列表,因此您必须使用不同的实例,而不是一遍又一遍地添加相同的视图。

如果你想让新添加的视图也响应回车键,你也可以为新视图设置相同的监听器:

if(event.getAction() == KeyEvent.ACTION_DOWN) {
    EditText addedView = (EditText) LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item,mContainer,false);
    addedView.setOnEditorActionListener(this);
    mContainer.addView(addedView);
}