Android - Recycler View focuses wrong holder on EditText click

I have a Recycler View to show a list of data and edit the text inside the Recycler View to allow the user to edit some number.

My app also resizes when the keyboard is displayed / hidden.

The application works great if I do not want to edit the elements that are located below. When I click to edit the text, it changes the screen size, shows the keyboard, focus holder, but it’s wrong: usually one before and sometimes 2.

I tried to track the problem and realized that the LinearLayoutMnager onFocusSearchFailed function returns an incorrect view.

Any idea why this could be?

Adapter

private final LinearLayoutManager linearLayoutManager; public Observable<double[][]> observable; private ObservableEmitter<double[][]> observe; List<Invoice> invoices = Collections.emptyList(); double[][] amounts; Context context; public AdjustInvoiceAdapter(List<Invoice> list, Context context, LinearLayoutManager linearLayoutManager) { this.invoices = list; this.context = context; this.linearLayoutManager = linearLayoutManager; amounts = new double[invoices.size()][1]; for (int i = 0; i < list.size(); i++) { amounts[i][0] = invoices.get(i).getAmountDue(); } observable = Observable.create(new ObservableOnSubscribe<double[][]> (){ @Override public void subscribe(ObservableEmitter<double[][]> emitter) throws Exception { observe = emitter; } }); } @Override public InvoiceHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_adjust_invoice_payment_amount, parent, false); InvoiceHolder holder = new InvoiceHolder(view); return holder; } @Override public void onBindViewHolder(InvoiceHolder holder, final int position) { holder.update(invoices.get(position), amounts[position][0]); holder.amount.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s.length() == 0 || (s.length() == 1 && (s.toString().charAt(0)) == '-')) amounts[position][0] = 0.0; else amounts[position][0] = Double.parseDouble(s.toString()); observe.onNext(amounts); } }); } @Override public int getItemViewType(int position) { return position; } @Override public int getItemCount() { return invoices.size(); } 

holder

 public class InvoiceHolder extends RecyclerView.ViewHolder { EditText amount; TextView invoiceAmountDue; TextView invoiceId; TextView invoiceDueDate; TextInputLayout textInputLayout; private NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.US); public InvoiceHolder(View itemView) { super(itemView); invoiceId = (TextView) itemView.findViewById(R.id.invoice_number_text_view); invoiceDueDate = (TextView) itemView.findViewById(R.id.invoice_due_text_view); invoiceAmountDue = (TextView) itemView.findViewById(R.id.invoice_amount_due_text_view); amount = (EditText) itemView.findViewById(R.id.editText); textInputLayout = (TextInputLayout) itemView.findViewById(R.id.text_input_layout); } void update(Invoice invoice, double currentAmount) { textInputLayout.setHint("(" + numberFormat.format(invoice.getAmountDue())+")"); invoiceId.setText(invoice.getinvoiceId()); invoiceDueDate.setText(invoice.getInvoiceDueDate()); amount.setText(currentAmount); } 

}

Layout (RecyclerView has no attributes)

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusableInTouchMode="true" android:focusable="true" android:clickable="true" android:foreground="?android:attr/selectableItemBackground" > <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:gravity="center_vertical"> <LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:layout_marginTop="4dp" android:orientation="vertical" android:paddingLeft="@dimen/activity_main_margin" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5"> <TextView android:id="@+id/invoice_number_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:text="TextView" android:textColor="@android:color/black" android:textSize="18sp" tools:layout_editor_absoluteX="7dp" tools:layout_editor_absoluteY="0dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal"> <ImageView android:id="@+id/red_warning_image_view" android:layout_width="15dp" android:layout_height="15dp" android:layout_marginRight="5dp" android:backgroundTint="@color/ebiz_red" android:tint="@color/ebiz_red" android:visibility="gone" app:srcCompat="@drawable/ic_error_black_24dp" /> <TextView android:id="@+id/invoice_due_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:maxLines="1" android:text="TextView" android:textColor="@color/ebiz_gray_medium" android:textSize="12sp" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="0dp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusableInTouchMode="true" android:layout_marginBottom="8dp" android:layout_marginEnd="16dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toRightOf="@+id/linearLayout4" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.511"> <android.support.design.widget.TextInputLayout android:id="@+id/text_input_layout" android:layout_width="100dp" android:layout_height="match_parent" tools:layout_editor_absoluteX="143dp" tools:layout_editor_absoluteY="0dp"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="numberDecimal" android:text="$100" android:focusableInTouchMode="true" android:textColor="@android:color/black" android:textSize="18sp" /> </android.support.design.widget.TextInputLayout> </LinearLayout> </android.support.constraint.ConstraintLayout> 

+5
source share

Source: https://habr.com/ru/post/1265893/


All Articles