android – How to implement a selector that applies to the entire list item

Question:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:orientation="horizontal">

  <!-- これはネットワーク経由で読み込む画像 -->
  <ImageView
      android:id="@+id/image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:src="@drawable/ic_launcher"/>

  <TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:layout_margin="8dp"
      tools:text="hogehoge"/>

</LinearLayout>

The above layout is set as the item layout of RecyclerView.
If you apply a selector to this, of course, only the background of the layout will change, so the ImageView will look the same.

ImageView also wants to apply the same selector effect as the background (an image that blends translucent black throughout).

It can be realized by using a selector for the background and ColorFilter for the image.

  • It is difficult to deal with complicated layouts (when a View with a background color is included in this layout, when the number of ImageViews increases, etc.)
  • Definition location is distributed in xml and code and difficult to maintain

There are problems such as.

How do you achieve filter-like functionality that can be applied to the entire layout?

* As a prerequisite, the target OS is 4.0-5.0, and Material Design will not be introduced this time.

Answer: Answer:

As another solution from fkm, how about using FrameLayout 's foreground ?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:foreground="@drawable/foreground_selector"
    android:clickable="true"
    android:focusable="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:orientation="horizontal">
        ...

You can also specify the android:drawSelectorOnTop attribute if you like ListView .
http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:drawSelectorOnTop

Scroll to Top