RecycleView点击效果

背景

Android5.0以上新增了波纹效果
在已有项目中没有使用AppCompatActivity,所以在item最外层容器中增加

android:foreground="?attr/selectableItemBackground"

之后,波纹效果依然无效

解决办法

在drawable-v21中新建一个xml文件item_spot_touch_bg.xml,代码如下

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/caldroid_lighter_gray">
    <item android:drawable="@color/white"/>
</ripple>

在itemview的最外层容器中,设置background为上述xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:foreground="?android:attr/selectableItemBackground"
    android:background="@drawable/item_spot_touch_bg" >
    ...
</LinearLayout>

注意事项

为了兼容Android5.0以下的系统,需要在drawable新建一个同名的xml文件item_spot_touch_bg.xml,代码如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/caldroid_lighter_gray" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@color/white" android:state_enabled="true" android:state_pressed="false"/>
    <item android:drawable="@color/white"></item>
</selector>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,907评论 25 708
  • Android开发中,和布局文件对应 View 一样,Drawable 文件也对应着一个类--Drawable。如...
    食梦兽阅读 7,670评论 2 15
  • Android 5.0中新增了ripple类型,即波纹效果这里要注意,波纹效果只在5.0以上的设备生效,要实现此种...
    DevWang阅读 8,742评论 7 52
  • 老家科目三教练发来信息:10月份驾考又加新规定啦,到时候又更难啦!想着此次回老家正好有空闲时间二十多天,且没有更重...
    琴韵2竹语阅读 204评论 2 1
  • 早上他跟我说,他昨晚做了一个噩梦,梦到他结婚了,新娘却不是我。 每天都有各种青春爱情题材的帖子粉墨登场,堕胎流产的...
    Iris_傅阅读 524评论 0 1

友情链接更多精彩内容