モノトーンの伝説日記

Apex Legends, Splatoon, Programming, and so on...

<mini> ripple (タップ時) 効果のつけ方 [Android, non-default control]

 ということで書く。

 Android Support Library で RecyclerView で ripple effect かけたいとき、ってあると思うんだけど、そのやり方書く。ちなみに旧機種では非 ripple で API 11 からしか使えない。たぶん互換用に style 書けばいいけどまだそこまでやってないので今回は割愛。追記で書くかも。

 簡単だけど、List 側じゃなくて子、つまり ListItem のテンプレート側に左右されるみたい。xaml 脳だと ListItem の箱と ListItem の中身を別々にする考えが身についていると思うけど、Android の場合は何も間接的にサポートするオブジェクトがなく、直系に子が表示されていると思えばいい。

 つまり、

+ ListView
  + ListViewItemContainer (ItemContainerStyle)
    + ListViewItemContent (ItemTemplate)

xaml だが、Android だと

+ RecyclerView
  + RecyclerViewItemView

って感じ。xaml の tree と違って直系なので、直径に対して effect 効果を発動する機能をフックする必要がある。

 子を以下のように改変すればよい。ちなみにこのテンプレートは Material Design - List - Text Only を参考に*1作成している。

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/title"
    android:layout_height="48dp"
    android:layout_width="fill_parent"
    android:paddingLeft="24dp"
    android:paddingTop="16dp"
    android:paddingRight="24dp"
    android:paddingBottom="16dp"
    android:background="?android:attr/selectableItemBackground"
    android:textColor="@color/abc_primary_text_material_light" />

 以上です。

*1:Dialog 向けなので左右が 24dp になっている