我在RecyclerView
项中有一个TextInputEditText,用户可以在其中输入描述。我想保存输入的文本,只要按下Enter键即可。因此,我添加了一个应对此进行处理的OnEditorActionListener。但是听者永远不会被触发。
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);
}
项目点击
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);
}
});
}
局部变量expandedPosition
和editPosition
仅保存RecyclerView
中的哪个元素被展开或当前正在编辑哪个元素。