键盘打开时保持State RecyclerView状态

时间:2020-11-12 11:24:03

标签: android xml kotlin

我遇到一种情况,RecyclerView有5个数据Horizontal,当我处于第5个数据并单击EditText时,键盘立即出现滚动回到第一个数据,我想在EditText上填写第5个数据时感到非常不安,如何防止这种情况发生,以使RecyclerView不会自动滚动到第1个数据?

AndroidManifest.xml

   <activity
       android:name=".MainActivity"
       android:screenOrientation="portrait"
       android:theme="@style/AppTheme.NoActionBar"
       android:windowSoftInputMode="stateHidden|adjustResize">    
   </activity>

HomeFragment.kt

  rvMenuProduct.apply{
      layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
      adapter = MyAdapter(Data)
  }

fragment_home_product.xml

<?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:orientation="vertical"
    android:background="@color/colorWhite">

    <TextView
        android:id="@+id/tvProductTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_10"
        android:layout_marginStart="@dimen/margin_10"
        android:fontFamily="@font/roboto_bold"
        android:textSize="@dimen/text_title"
        android:textColor="@color/colorPrimary"
        android:text="@string/home_product" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvMenuProduct"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:overScrollMode="never"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/fragment_home_product_item" />
</LinearLayout>

我的编辑文本

<EditText
    android:id="@+id/etCartProductQuantity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="0dp"
    android:layout_marginBottom="0dp"
    android:background="@drawable/home_product_item_qty"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:hint="@string/product_qty_default"
    android:imeOptions="actionDone"
    android:importantForAutofill="no"
    android:inputType="number"
    android:text="@string/product_qty_zero"
    android:textAlignment="center"
    android:textColor="@color/colorBlack"
    android:textCursorDrawable="@null" />

0 个答案:

没有答案