Android - Recycler View在EditText上单击错误的Holder

时间:2017-03-24 21:16:39

标签: keyboard android-recyclerview resize focus android-viewholder

我有一个Recycler View来显示数据列表,并在Recycler View中编辑文本以允许用户编辑一些数字。

我的应用程序还会在显示/隐藏键盘时调整活动大小。

除非我想编辑位于底部的项目,否则该应用程序效果很好。当我点击编辑文字时,它调整屏幕大小,显示键盘,焦点持有人但错误的一个:通常一个之前,有时一个2.

我试图跟踪问题,并了解LinearLayoutMnager的onFocusSearchFailed函数返回错误的视图。

知道为什么会这样吗?

适配器

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();
}

持有人

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);
}

}

布局(RecyclerView没有任何属性)

<?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>

0 个答案:

没有答案