一、問題來源
在實(shí)現(xiàn)選RecycleView 中選中item高亮的效果時(shí),發(fā)現(xiàn)必須點(diǎn)擊同一個(gè)Item兩次才可以觸發(fā)綁定在itemView上的點(diǎn)擊函數(shù)。
在item的布局文件設(shè)置如下:
<?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>
二、問題解決
查閱相關(guān)資料皿渗,發(fā)現(xiàn)原來是在layout中定義的android:focusableInTouchMode="true"
出了問題
該參數(shù)會(huì)使得第一次點(diǎn)擊動(dòng)作是獲得焦點(diǎn),第二次點(diǎn)擊才觸發(fā)點(diǎn)擊動(dòng)作轻腺。所以應(yīng)該將其刪除或者設(shè)置為false