setBackgroundResource与setBackground的行为与RippleDrawable不同

Geo*_*geP 5 android rippledrawable

我想设置RippleDrawable一个列表视图行的背景.使用时view.setBackgroundResource(R.drawable.ripple)一切正常.在尝试时

view.setBackground(view.getContext().getDrawable(R.drawable.ripple))
Run Code Online (Sandbox Code Playgroud)

它可以工作,但是当快速触摸项目时没有显示波纹(我也有项目的激活/检查状态),它只在我按下视图时出现.

View类中的setBackgroundResource方法如下所示:

if (resid != 0 && resid == mBackgroundResource) {
    return;
}

Drawable d = null;
if (resid != 0) {
    d = mContext.getDrawable(resid);
}
setBackground(d);

mBackgroundResource = resid;
Run Code Online (Sandbox Code Playgroud)

所以基本上我正在尝试手动完成的事情.

注意:我想使用setBackground方法,因为我想以编程方式创建RippleDrawable.

有没有人知道为什么会这样?

Gun*_*son 1

不要在 row 上设置波纹,而是使用以下属性View将其设置:ListViewlistSelector

<ListView
    ...
    android:listSelector="@drawable/ripple" />
Run Code Online (Sandbox Code Playgroud)

您可以通过编程方式设置它mListView.setSelector(...)。