有时不显示波纹效果 [英] Ripple effect does not show up sometimes

查看:107
本文介绍了有时不显示波纹效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个可绘制的涟漪图,并将其设置为listview项目视图背景的背景.几乎可以正常工作,但有时在印刷机上不会出现波纹效果.

I defined a ripple drawable and set it as the background for the listview item view's background. It almost works fine but the ripple effect just sometimes does not show up upon press.

这是涟漪可绘制代码:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item>
        <selector>
            <item android:state_activated="true">
                <shape><solid android:color="?android:attr/colorButtonNormal"/>
                </shape>
            </item>

            <item>
                <shape><solid android:color="@android:color/transparent"/>
                </shape>
            </item>
        </selector>
    </item>
</ripple>

我有一个导航抽屉,它使用此drawable作为抽屉项目的背景:

And I have a navigation drawer which uses this drawable as the background for drawer items:

<?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="@dimen/navigation_menu_height"
    android:orientation="horizontal"
    android:background="@drawable/listItemBackground">
    ....    
</RelativeLayout>

但是,每次我打开应用程序时,默认选择的项目在按下时都不会显示波纹效果.但是,当我选择另一个项目然后再次选择它之后,一切都会很好.

However every time when I open the app, the default selected item does not show the ripple effect when I press it. But after I select another item and then select it again, everything would just goes fine.

我将完全相同的ListView用于另一个几乎相同的ListView,不同的是选择模式是多个.而且这一次,只要选中它们,它们在按下时就不会显示涟漪效应.

I use exactly the same for another almost the same ListView, except the choice mode is multiple. And this time, the items will not show the ripple effect upon press as long as they are selected.

在另一个未指定选择模式的listView中,只有第一项显示涟漪效应,其他项的行为与Android较低版本中的行为相同.

In another listView, which does not specify choice mode, only the first item shows ripple effect, and others behave the same as in lower Android versions.

我真的不知道如何使它适用于所有情况.也许真的与ListView的选择模式有关吗?

I really have no idea of how I can make it work for all scenarios. maybe is it really relates to the choice mode of the ListView?

==== 编辑 ====

====EDIT====

我刚刚为所有这三个listViews添加了属性drawSelectorOnTop,现在清除了前两个问题.但是对于第三个,它现在变成这样:

I just added property drawSelectorOnTop for all these three listViews and now the first two problems are cleared. However for the third one, it now becomes like this:

除第一个显示两个涟漪的物品外,涟漪效果在所有物品上效果都很好!其中一个从我的手指按下位置开始,而另一个总是从中指开始!

Ripple effect works good on all items except the first one which shows two ripples! One of them starts from where my finger presses while the other always starts from the middle!

==== 再次编辑 ==== 好的,现在我将可绘制对象调整为将激活状态移到涟漪节点之外,并与listview的drawSelectorOnTop属性一起移动,问题现在完全消失了.

====EDIT AGAIN==== OK, now I tweak the drawable to move the activated status outside of ripple node, together with the drawSelectorOnTop attribute for the listview, the problem's now completely gone.

推荐答案

好的,经过一番尝试和错误后,我终于使它工作了.

OK, After some try and error, I have finally get it work.

首先,将激活状态移出波纹节点:

First, move the activated state out of ripple node:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true">
        <shape>
            <solid android:color="?android:attr/colorButtonNormal"/>
        </shape>
    </item>
    <item>
        <ripple android:color="?android:attr/colorControlHighlight">
            <item>
                <shape>
                    <solid android:color="@android:color/transparent"/>
                </shape>
            </item>
        </ripple>
    </item>
</selector>

之后,将使用drawable作为项目背景的ListView的drawSelectorOnTop设置为true.

After that, set drawSelectorOnTop to true for the ListView that use the drawable as item background.

现在,涟漪效应可以完美发挥作用了.

Now the ripple effect can work perfectly.

这篇关于有时不显示波纹效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆