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。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值