我发现,如果在RecylerView
的项上应用android:stateListAnimator
,则调用adapter.notifyDataSetChanged
将会导致某些RecylerView
项出现不必要的闪烁效果(奇怪的是,并非所有项都会出现该问题)。
这是我的RecylerView
项:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:stateListAnimator="@anim/lift_up"
android:background="@drawable/white" >
...
</LinearLayout>
@anim/lift_up
的定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="8dip"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="4dip"
android:valueType="floatType" />
</item>
</selector>
并且@drawable/white
被定义为
<drawable name="white">#ffffffff</drawable>
当我调用adapter.notifyDataSetChanged
时,RecyclerVIew的最后5个可见项会出现奇怪的闪烁效果。(屏幕上总共有10个可见项)
这个问题只会在API 21及以上的版本中出现,因为只有API 21支持android:stateListAnimator
这是一个bug吗?还是我漏掉了什么?
完整的最小可行代码可以从https://github.com/yccheok/RecyclerViewTutorial/tree/4763879598864233a8e6544fe240c3fb34a15b73下载。