Android进阶之解决RecyclerView notifyItem闪屏问题

来源:互联网 发布:中国阶级 知乎 编辑:程序博客网 时间:2024/06/05 05:52

1 RecyclerView刷新方法

1.1操作内容

ListView的getView方法的渲染数据部分的代码相当于onBindViewHolder(),如果调用adapter.notifyDataSetChanged()方法,会重新调用onBindViewHolder()方法。

1.2其他刷新方法

除了adapter.notifyDataSetChanged()这个方法之外,新的Adapter还提供了其他的方法,如下:

//刷新所有public final void notifyDataSetChanged();//position数据发生了改变,那调用这个方法,就会回调对应position的onBindViewHolder()方法了public final void notifyItemChanged(int position);//刷新从positionStart开始itemCount数量的item了(这里的刷新指回调onBindViewHolder()方法)public final void notifyItemRangeChanged(int positionStart, int itemCount);//在第position位置被插入了一条数据的时候可以使用这个方法刷新,注意这个方法调用后会有插入的动画,这个动画可以使用默认的,也可以自己定义public final void notifyItemInserted(int position);//从fromPosition移动到toPosition为止的时候可以使用这个方法刷新public final void notifyItemMoved(int fromPosition, int toPosition);//批量添加public final void notifyItemRangeInserted(int positionStart, int itemCount);//第position个被删除的时候刷新,同样会有动画public final void notifyItemRemoved(int position);//批量删除public final void notifyItemRangeRemoved(int positionStart, int itemCount);

2 闪屏问题

2.1 问题描述

RecyclerView做了一个notifyItemChanged()的操作,功能都顺利实现,问题当前Item闪烁,QA甚至为此提了Bug。

2.2 问题原因

闪烁主要由于RecyclerView使用的默认的动画导致的,所以解决的方法就是修改默认的动画。

2.3 问题解决

2.3.1 最简单方法

DefaultItemAnimator继承自SimpleItemAnimator,里面有个方法是:

    /**     * Sets whether this ItemAnimator supports animations of item change events.     * If you set this property to false, actions on the data set which change the     * contents of items will not be animated. What those animations do is left     * up to the discretion of the ItemAnimator subclass, in its     * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} implementation.     * The value of this property is true by default.     *     */    public void setSupportsChangeAnimations(boolean supportsChangeAnimations) {        mSupportsChangeAnimations = supportsChangeAnimations;    }

只要设置为false,就可以不显示动画了,也就解决了闪烁问题。 关键代码:

((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

2.3.2 修改默认的动画方法

//1.定义动画类public class NoAlphaItemAnimator extends RecyclerView.ItemAnimator {}//2.将DefaultItemAnimator类里的代码全部copy到自己写的动画类中,然后做一些修改。//3.首先找到private void animateChangeImpl(final ChangeInfo changeInfo) {}方法。//4.找到方法里这两句代码:4.1 去掉alpha(0)oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() {...}).start();oldViewAnim.setListener(new VpaListenerAdapter() {...}).start();4.2 去掉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();//5.最后使用修改后的动画recyclerView.setItemAnimator(new NoAlphaItemAnimator());
1 0
原创粉丝点击