[Android] 设置OnClickListener后需要点击两次才能触发onClick方法

一、问题来源

在实现选RecycleView 中选中item高亮的效果时,发现必须点击同一个Item两次才可以触发绑定在itemView上的点击函数。
在item的布局文件设置如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:focusable="true"
    android:clickable="true"
    android:focusableInTouchMode="true">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />
</LinearLayout>

二、问题解决

查阅相关资料,发现原来是在layout中定义的android:focusableInTouchMode="true"出了问题
该参数会使得第一次点击动作是获得焦点,第二次点击才触发点击动作。所以应该将其删除或者设置为false

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容