闪烁主要由于RecyclerView使用的默认的动画导致的,所以解决的方法就是修改默认的动画。
首先把默认的动画(DefaultItemAnimator)代码复制到自定义的MyItemAnimator类里面。
接着找到animateChangeImpl方法修改其中的两行代码。
// 去掉alpha(0) oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() {...}).start(); oldViewAnim.setListener(new VpaListenerAdapter() {...}).start(); // 去掉alpha(1) newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()). alpha(1).setListener(new VpaListenerAdapter() {...}).start(); newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()). setListener(new VpaListenerAdapter() {...}).start();
最后使用修改后的动画。
recyclerView.setItemAnimator(new MyItemAnimator());