0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Material Components for AndroidのChipに発生するRippleEffectを消す

Last updated at Posted at 2019-04-01

概要

リストアイテム内にChipを表示している画面で、リストアイテムをタップするとChipにもRippleEffectが発生していました。
動作上は問題ありませんが、見た目に少し違和感を感じました。
リストアイテムをタップしてもChipにはRippleEffectが発生しないように修正しました。

screenshot1.gif

  • rippleColorを透明にする
  • clickListenerをnullにするとRippleEffectは発生しないがElevationが効かなくなる

詳細

rippleColorを透明にする

app:rippleColor="@android:color/transparent"を指定するとRippleEffectの色を透明にできます。

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checkable="false"
    android:clickable="false"
    app:chipIconEnabled="false"
    app:closeIconEnabled="false"
    app:rippleColor="@android:color/transparent" />

screenshot2.gif

clickListenerをnullにするとRippleEffectは発生しないがElevationが効かなくなる

挙動を見たところ親のリストアイテムで発生したクリックイベントが子のChipに伝搬しているように思えました。
そのため、試しにChipのクリックリスナーにnullを設定してクリックイベントが伝搬しないようになるか試してみました。
結果的にRippleEffectは発生しなくなりましたが、タップ時のElevationがなくなってしまいました。
また、これまではandroid:clickable="false"でクリック不可にしていたChipがクリックできるようになってしまいました。

(LayoutInflater.from(context).inflate(R.layout.chip, this, false) as Chip).setOnClickListener(null)

screenshot3.gif

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?