I'm making a feature that pops up an empty dialog and enters comments.
The dialog consists of a RecyclerView
.
And items consist of EditText
.
Only one line of comments can be entered.
Pressing EnterKey
dynamically creates the next comment input field.
However, when i press the enter key, two items
are created.
Using the Toast message
, I noticed that OnAddComment()
was called twice.
Even after debugging, I am not sure why there are two.
What's the problem?
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);
}
}
It is receiving callback 2 times. One for action ACTION_DOWN
& one for action ACTION_UP
when he is pressing the enter button.
if(listener != null) { if( event.getAction() == KeyEvent.ACTION_DOWN) { listener.OnAddComment(getAdapterPosition()); } }
So add comments only when action is ACTION_DOWN
.