如何解决RecyclerView 中的最后一项 Edittext 在点击时失去焦点,而是专注于第一个 Edittext
我有一个包含 EditTexts 的水平 RecyclerView。每当我滚动到列表中的最后一个项目并单击那里的 edittext 字段时,列表就会滚动回列表顶部,而第一个项目的 edittext 会获得焦点。
如果有帮助,水平 RecyclerView 的父级恰好是在运行时填充的 LinearLayout,其中包含相同水平 RecyclerView 的多个实例。
我尝试在 1) edittext 本身、2) recyclerView 和 3) LinearLayout 的 xml 中应用focusable、focusableInTouchMode 和descendantFocusability 的各种实现。
这是最外层布局的xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_gray7">
<!-- Some omitted layouts -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/catalog_shop_search_layout">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/catalog_shop_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent">
<!-- Other omitted views -->
<!-- Dynamic ViewGroup populated dynamically -->
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/catalog_featured_category_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_normal"
android:orientation="vertical" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是动态填充的水平列表的 xml:
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_gray7"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/margin_small" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/catalog_category_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:paddingStart="@dimen/margin_normal"
android:paddingTop="@dimen/margin_normal"
android:paddingBottom="@dimen/margin_normal">
<!-- Some omitted views -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/catalog_category_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_normal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toBottomOf="@+id/catalog_category_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/margin_small_v2" />
</androidx.appcompat.widget.LinearLayoutCompat>
这是进入回收视图的适配器的xml
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/catalog_product_layout"
android:layout_width="@dimen/cart_catalog_width"
android:layout_height="wrap_content"
android:background="@color/white"
android:onClick="@{() -> handler.onItemClicked(product)}"
android:orientation="vertical"
android:padding="@dimen/margin_normal">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/catalog_product_image"
android:layout_width="@dimen/cart_catalog_image_size"
android:layout_height="@dimen/cart_catalog_image_size"
android:adjustViewBounds="true"
android:glide="@{product.image}"
android:scaleType="fitXY"
app:srcCompat="@drawable/placeholder_item" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/catalog_product_price_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/text_size_large"
android:orientation="horizontal"
android:paddingTop="@dimen/margin_very_small"
android:paddingBottom="@dimen/margin_very_small">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/catalog_product_price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="2dp"
android:layout_weight="0.5"
android:autoSizeMaxTextSize="@dimen/text_size_small_v2"
android:autoSizeMinTextSize="10dp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
android:gravity="start|center_vertical"
android:singleLine="true"
android:text="₱ 33.00"
android:textColor="@color/red2"
android:textSize="@dimen/text_size_small_v2"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/catalog_product_old_price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_weight="0.5"
android:autoSizeMaxTextSize="@dimen/text_size_small_v2"
android:autoSizeMinTextSize="10dp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
android:gravity="start|center_vertical"
android:singleLine="true"
android:text="₱ 88.00"
android:textColor="@color/gray_blue"
android:textSize="@dimen/text_size_small_v2" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/catalog_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="4"
android:maxLines="4"
android:paddingBottom="@dimen/margin_small"
android:textColor="@color/dark_gray2"
android:textSize="@dimen/text_size_small_v2"/>
<!-- Add to cart-->
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/catalog_product_add_to_cart"
android:layout_width="match_parent"
android:layout_height="32dp"
android:background="@color/light_gray7"
android:gravity="center"
android:onClick="@{() -> handler.onAddToCartClicked(product)}"
android:padding="@dimen/margin_small"
android:text="ADD TO CART"
android:textColor="@color/gray_blue3"
android:textSize="@dimen/text_size_small"
android:textStyle="bold" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/catalog_product_item_added_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="@dimen/margin_small"
android:visibility="gone">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/catalog_product_item_added_icon"
android:layout_width="14dp"
android:layout_height="14dp"
app:srcCompat="@drawable/ic_check_rounded" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/catalog_product_item_added_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:text="ITEM ADDED"
android:textColor="@color/green3"
android:textSize="@dimen/text_size_small"
android:textStyle="bold" />
</androidx.appcompat.widget.LinearLayoutCompat>
<!-- With Quantity UI -->
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/catalog_product_quantity_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone">
<com.google.android.material.button.MaterialButton
android:id="@+id/catalog_product_quantity_minus"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:insetTop="0dip"
android:insetBottom="0dip"
android:minWidth="0dip"
android:minHeight="0dip"
android:onClick="@{() -> handler.onSubQuantityClicked(product)}"
android:padding="@dimen/margin_small"
app:cornerRadius="0dip"
app:icon="@drawable/ic_item_sub_cart"
app:iconGravity="textStart"
app:iconPadding="0dp" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/catalog_product_quantity"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@drawable/shape_item_quantity_outline"
android:gravity="center"
android:imeOptions="actionDone"
android:includeFontPadding="false"
android:inputType="number"
android:maxLength="3"
android:padding="0dp"
android:text="000"
android:textColor="#4a4a4a"
android:textSize="@dimen/text_size_small_v2" />
<com.google.android.material.button.MaterialButton
android:id="@+id/catalog_product_quantity_add"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:insetTop="0dip"
android:insetBottom="0dip"
android:minWidth="0dip"
android:minHeight="0dip"
android:onClick="@{() -> handler.onAddQuantityClicked(product)}"
android:padding="@dimen/margin_small"
app:cornerRadius="0dip"
app:icon="@drawable/ic_item_add_cart"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconTint="#016d3c" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。