对ListView项的涟漪效应

Cly*_*nux 8 android android-layout android-activity

我是Android开发的新手.我正在尝试开发适用于Android的短信应用.我成功阅读了收件箱和联系人并将其显示在一个ListView项目中.现在,我想要实现的是ListView每次点击或触摸或选择这些项目时都会产生连锁反应.我使用listSelector但似乎它不起作用.如果有人可以帮助我,下面是我的代码:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/msglist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="true" 
        android:listSelector="#777777" />
Run Code Online (Sandbox Code Playgroud)

小智 17

你打算在你的Adapterfor中夸大一个布局ListView吧?然后,您可以转到layout.xml并设置android:background="?android:selectableItemBackground"为根元素.

注意:这样做只能在API 21(Lollipop)之上发生连锁反应.在Lollipop下,当您触摸该项目时,将出现一个透明的蓝色图层并覆盖您的源内容,这将显示为选择状态.

  • 如果你的根元素是`FrameLayout`或它的子类,比如`CardView`,你可以设置`android:foreground ="?android:attr/selectableItemBackground"`.因此,您可以通过设置背景轻松设置颜色. (5认同)