0

I have a TextInputEditText inside an Item RecyclerView where the user can enter a description. I want to save the entered text as soon as enter gets pressed. So I added an OnEditorActionListener which should handle this. But the listener never gets triggered.

OnBind-Method of RecyclerView

@Override
public void onBindViewHolder(final HistoryViewHolder holder, int position) {
    final Bill bill = billsToDisplay.get(position);

    displayBillType(bill, holder);
    displayDescription(bill.getDescription(), holder);
    displayDateAndAmount(bill, holder);
    displayCategoryImage(bill, holder);

    setupBillActionButtonsClickListeners(holder, bill);
    if(allowToEditBill && recyclerView != null){
        setupOnItemClickAction(holder, position);
    }
}

TextInputEditText

    <android.support.design.widget.TextInputLayout
    android:id="@+id/til_item_history_edt_container"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="10"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/edt_item_history_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionDone" />

</android.support.design.widget.TextInputLayout>

OnEditorActionListener

private void setupForDescriptionEdit(HistoryViewHolder holder){
    editPosition = expandedPosition;

    holder.mEdtEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            return false;
        }
    });

    showKeyboardForEditInput(holder.mEdtEdit);
    notifyItemChanged(expandedPosition);
}

OnClick of Items

private void setupOnItemClickAction(HistoryViewHolder holder, int position){
    final boolean isExpanded = position == expandedPosition;
    final boolean isEditModeActive = position == editPosition;

    holder.mLlBillActionButtonsContainer.setVisibility(isExpanded && !isEditModeActive ? View.VISIBLE : View.GONE);
    holder.mLlBillEditElementsContainer.setVisibility(isExpanded && isEditModeActive ? View.VISIBLE : View.GONE);
    holder.itemView.setActivated(isExpanded || isEditModeActive);

    holder.itemView.setOnClickListener(v -> {
        int previousExpandedPosition = expandedPosition;
        expandedPosition = isExpanded ? -1 : position;

        if (!isEditModeActive){
            editPosition = -1;
        }

        if (previousExpandedPosition != -1){
            notifyItemChanged(previousExpandedPosition);
        }

        if (expandedPosition != -1){
            notifyItemChanged(expandedPosition);
        }
    });
}

The local variables expandedPosition and editPosition just save which element in the RecyclerView is expanded or which one is currently edited.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
eli2003
  • 400
  • 5
  • 13

0 Answers0