下の画像のような RecyclerView があります。
円はonClickListenerを持つLinearLayoutにあります。円をクリックすると、期待どおりの動作になります。
RecyclerView の convertView には onClickListener が設定されています。RecyclerView の項目をクリックすると、予期しない動作になります。予想される動作は、アイテム全体がハイライトでフォーカスされていることですが、円の周りの黒い部分はフォーカスされていません。
私はこれを長い間検索しましたが、何も得られませんでした。どうすれば解決できますか?
これは RecyclerView のアイテム レイアウトです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_marginBottom="1dp"
android:orientation="horizontal"
android:background="@drawable/action_color"
android:layout_width="match_parent"
android:layout_height="55dp">
<TextView
style="@style/FormFont"
android:layout_weight="7"
android:id="@+id/no"
android:text="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/parent_status"
android:background="@drawable/action_color"
android:layout_weight="7"
android:gravity="center"
android:padding="10dp"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:layout_centerInParent="true"
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/status_progressbar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/switch_retry"
android:text="@string/retry"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
style="@style/FormFont"
android:id="@+id/temperature"
android:layout_weight="8"
android:text="20"
android:layout_width="0dp"
android:layout_height="match_parent" />
<TextView
style="@style/FormFont"
android:id="@+id/humidity"
android:layout_weight="8"
android:text="15"
android:layout_width="0dp"
android:layout_height="match_parent" />
<LinearLayout
android:background="@drawable/action_color"
android:id="@+id/parent_mode"
android:layout_weight="10"
android:orientation="vertical"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/mode"
android:layout_width="33dp"
android:layout_height="33dp"
android:scaleType="fitXY"
/>
<TextView
android:text="定时模式"
android:textSize="12dp"
android:id="@+id/mode_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:visibility="gone"
android:id="@+id/progressbar"
android:layout_weight="10"
android:orientation="vertical"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="6"
android:layout_width="0dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_height="match_parent">
<CheckBox
android:visibility="gone"
android:focusableInTouchMode="false"
android:focusable="false"
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/arrow"
android:src="@mipmap/ic_right_arrow"
android:layout_width="30dp"
android:layout_height="30dp" />
</LinearLayout>
</LinearLayout>
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_spot,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@drawable/action_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorLightGray"/>
<item android:state_activated="true" android:drawable="@color/colorLightGray"/>
<item android:state_selected="true" android:drawable="@color/colorLightGray"/>
<item android:drawable="@android:color/white"/>
</selector>
statusParent に onClickListener を設定しないと (LinearLayout が円のイメージをラップした場合、アイテムをクリックすると statusParent LinearLayout がハイライトされます 。statusParent はクリック可能なので、ハイライトされないと思います。
// viewHolder.statusParent is the LinearLayout of the Id R.id.status_parent.
// viewHolder.statusParent.setOnClickListener(this);