android - RecyclerView 选择器中的 ConstraintLayout

标签 android android-recyclerview android-selector android-constraintlayout

我在适配器项中使用 ConstraintLayout 作为根(使用 RecyclerView)。我的问题是我无法让选择器在 Lollipop 之前的设备上工作。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="@dimen/material_cell_height"
android:foreground="@drawable/selector_cell"
app:layout_constraintLeft_toLeftOf="parent" 
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
    android:id="@+id/textview_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="@dimen/material_content_padding"
    android:paddingStart="@dimen/material_content_padding"
    android:textAppearance="@style/TextAppearance.Cell.Body2"
    android:transitionName="@string/transition_toolbar"
    android:translationZ="1dp"
    app:layout_constraintBottom_toTopOf="@+id/textview_author"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/btn_overflow"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="packed"
    tools:ignore="UnusedAttribute,NewApi,RtlSymmetry,PrivateResource"
    tools:text="The Best Product" />

<TextView
    android:id="@+id/textview_author"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="@dimen/material_content_padding"
    android:paddingStart="@dimen/material_content_padding"
    android:textAppearance="@style/TextAppearance.Cell.Body"
    android:translationZ="1dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textview_title"
    tools:text="John Doe and Band" />

<ImageView
    android:id="@+id/imageview_cover"
    android:layout_width="42dp"
    android:layout_height="42dp"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:scaleType="centerCrop"
    android:translationZ="1dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:src="@mipmap/ic_launcher" />


<ImageButton
    android:id="@+id/btn_overflow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?selectableItemBackgroundBorderless"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:translationZ="1dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/textview_title"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_more_vert_black_24px" />


</android.support.constraint.ConstraintLayout>

这是我的 Lollipop 前设备选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_window_focused="false" />
<item android:drawable="@color/cell_touch_effect" android:state_checked="true" />
<item android:drawable="@color/cell_touch_effect" android:state_activated="true" />
<item android:drawable="@color/cell_touch_effect" android:state_selected="true" />
</selector>

有什么解决这个问题的建议吗?

最佳答案

您忘记将这些标签添加到您的 ConstraintLayout 中:

机器人:点击=“真” 机器人:可聚焦=“真”

关于android - RecyclerView 选择器中的 ConstraintLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40610851/

相关文章:

android - Android 应用程序中的 Piwik 跟踪

android - 在 Activity 之外显示对话

javascript - 手机锁定时 Socket.io 失去连接

Android 应用程序无法与 AdMob 一起运行

android - 为什么recyclerview android中两行(消息)之间有这么多空间

java - 在 recyclerview 中滚动时内存使用量增加

android - 如何从布局 View 中触发选择器?

android - 在嵌套的 RecyclerView 中保存滚动状态

安卓选择器。按下后如何进入正常状态?

android - GridView 项目选择器选中状态未出现