【问题标题】:Ripple effect weird behavior in ListViewListView 中的涟漪效应怪异行为
【发布时间】:2015-08-19 18:09:59
【问题描述】:

我已经使用<ripple> 元素为ListView 中的每个项目实现了涟漪效果。当我触摸(选择)ListView 中的每个项目时,我得到了想要的涟漪效果。但是当我滚动ListView 然后再次选择任何项目时,波纹消失得非常快(几乎不明显)。我不知道为什么波纹在一些ListView 项目上看起来很好,而在一些项目上却很奇怪。

我自定义的波纹布局如下所示(ripple_background.xml)

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/light_black_overlay">
    <item>
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/background_light" />
        </shape>
    </item>
</ripple>

ListView内的项目布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="48dp"
    android:minHeight="?android:listPreferredItemHeight"
    android:orientation="horizontal"
    android:background="@drawable/ripple_background"> <!--My ripple layout-->

    <TextView
        android:id="@+id/someText
        android... />

    <ImageView
        android:id="@+id/someImage
        android... />

</RelativeLayout>

有没有人在你的ListView 中遇到过这种奇怪的连锁反应?知道 ListView 的回收机制是否与此有关吗?谢谢。

【问题讨论】:

    标签: android android-listview material-design listviewitem rippledrawable


    【解决方案1】:

    不要将涟漪效应传递给列表中的每个项目,试试这个:

    <ListView
            android:id="@+id/yourListView"
            ...
            android:listSelector="@drawable/ripple_background" />
    

    【讨论】:

    • 谢谢赛义德,这对我有用。但是,如果我为列表视图中的每个项目使用波纹背景,您知道为什么它不起作用吗?
    • 实际上有一种方式可以为每个项目提供波纹,我不确定,但我认为您应该创建一个自定义适配器,并在绑定视图时检查项目是否需要显示波纹(通过存储每个项目的状态)和其他一些类似的处理。
    猜你喜欢
    • 2019-12-07
    • 2017-05-23
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 2020-05-30
    相关资源
    最近更新 更多