我不知道为什么这个方法被调用了两次

I don't know why this method is called twice

我正在制作一个弹出空对话框并输入评论的功能。

对话框由 RecyclerView.

项目包括 EditText

只能输入一行评论

EnterKey 动态创建下一个评论输入字段。

但是,当我按下回车键时,会创建 two items

使用 Toast message,我注意到 OnAddComment() 被调用了两次。

调试了也不知道为什么有两个

有什么问题?

writing_comment_item.xml

<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"/>

WritingCommentDialogFragment.java

public class WritingCommentDialogFragment extends DialogFragment {
    List<String> items;
    WritingCommentAdapter commentAdapter;
    RecyclerView comment_rv;
    LinearLayoutManager layoutManager;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);

        initViews(view);
        prepareForRecyclerView();
        commentAdapter.setOnWrtingCommentListener(new WritingCommentAdapter.OnWritingCommentListener() {
            @Override
            public void OnAddComment(int pos) {
                items.add("TEST");
                commentAdapter.addItems(items, pos);
            }
        });

        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();
        setDialogSize();
    }
    private void setDialogSize() {
        getDialog().getWindow().setLayout(1000, 1000);
    }

    private void initViews(View view) {
        comment_rv = view.findViewById(R.id.comment_rv);
    }

    private void prepareForRecyclerView() {
        commentAdapter = new WritingCommentAdapter();
        layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, true);
        comment_rv.setLayoutManager(layoutManager);
        comment_rv.setAdapter(commentAdapter);
        items = new ArrayList<>();
        items.add(null);
        commentAdapter.addItem(items);
    }
}

Adapter.java

public class WritingCommentAdapter extends RecyclerView.Adapter<WritingCommentAdapter.ViewHolder> {
    List<String> commentItems;
    OnWritingCommentListener listener;
    Context context;

    public void setOnWrtingCommentListener(OnWritingCommentListener listener) {
        this.listener = listener;
    }

    public void addItem(List<String> items) {
        commentItems = items;
    }

    public void addItems(List<String> items, int pos) {
        commentItems = items;
        notifyItemInserted(pos);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.writing_comment_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
 
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        EditText comment;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            comment = itemView.findViewById(R.id.comment_edit);

            comment.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override // IME Option NONE ( ENTER KEY)
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if(listener != null) {
                        listener.OnAddComment(getAdapterPosition());

                    }
                    return true;
                }
            });
        }

        public void setItem(String str) {
            comment.setText(str);
        }
    }

    public interface OnWritingCommentListener {
        public void OnAddComment(int pos);
    }
}

收到回调2次。一个用于操作 ACTION_DOWN,一个用于操作 ACTION_UP 当他按下回车按钮时。

if(listener != null) { if( event.getAction() == KeyEvent.ACTION_DOWN) { listener.OnAddComment(getAdapterPosition()); } } 

因此仅在操作为 ACTION_DOWN 时添加评论。