RecyclerView 内item点击失效

 

 例如

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/content_one"
    android:layout_width="match_parent"
    android:layout_height="@dimen/normal_100dp"
    android:paddingTop="@dimen/normal_0dp"
    android:paddingRight="@dimen/normal_24dp"

    >
    <TextView
        android:id="@+id/tv_location"
        android:layout_width="@dimen/normal_480sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/normal_16dp"
        android:maxLines="2"
        android:inputType="text"
        android:textColor="@color/color_222222"
        android:textSize="@dimen/normal_24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/view_01"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="广东省深圳市宝安区留仙二路庭威工业园二栋三楼"

        />
</androidx.constraintlayout.widget.ConstraintLayout>

item布局如下,当点击后无反应。需要把android:inputType=“text”去掉,就可以点击了!

原因:

官方是这样说明的:

Attribute android:inputType should not be used with <TextView>: Change element type to <EditText> ? less... (Ctrl+F1)

Using a <TextView> to input text is generally an error, you should be using <EditText> instead. EditText is a subclass of TextView, and some of the editing support is provided by TextView, so it's possible to set some input-related properties on a TextView. However, using a TextView along with input attributes is usually a cut & paste error. To input text you should be using <EditText>.

This check also checks subclasses of TextView, such as Button and CheckBox, since these have the same issue: they should not be used with editable attributes.

Issue id: TextViewEdits

结合以上说明,android:inputType的属性只对EditText有效,我认为EditText本身有抢夺焦点的作用,给TextView设置android:inputType的属性后可能也会导致焦点抢夺,导致点击“Text”文本并不会触发条目点击事件,而是被自身消耗掉了。也可以理解为点击事件被屏蔽掉了。

这里,要感谢下我的朋友margintop,他后来发现:如果已经设置了android:inputType的属性,再设置两个属性android:clickable和android:longClickable,这两个属性都设置成false,也可以达到预期的效果。也就是说android:inputType改变了那两个属性的默认值,把false改成了true。

 

 

上一篇:Elasticsearch 类比 mysql 实现 in and like or


下一篇:2021-02-06 SLF No SLF4J providers were found.