当在Recyclerview中的item项设置Ripple背景的drawable时, 如果点了item之后,很快就调用notifyDataSetChanged()
;会发现波纹效果Ripple动画好像不被触发, 但是如果用手按住该item不动, 则会完整的显示Ripple动画, 而这种情况正是notifyDataSetChanged();
这个方法阻断了Ripple动画的效果.
如文档中所说
RecyclerView will attempt to synthesize visible structural change events for adapters that report that they have stable IDs
when this method is used. This can help for the purposes of animation and visual object persistence but individual item views will still need to be rebound and relaid out.
在外层初始化adapter的时候, 设置setHasStableIds(boolean hasStableIds)
方法为true
, 如下
adapter.setHasStableIds(true);
//给Recyclerview设置adapter要写在此方法之后
mRecyclerView.setAdapter(adapter);
这样, 无论如何点击item都会显示完整的Ripple效果了.