Android RecycleView加载数据时,View闪烁

在项目中遇到一个问题,用RecycleView+databing组合加载或者刷新数据的时候,View总是会闪烁一下。

查询了很多文档,有的写的是关闭动画,用如下代码:

recyclerView.getItemAnimator().setAddDuration(0);
        recyclerView.getItemAnimator().setChangeDuration(0);
        recyclerView.getItemAnimator().setMoveDuration(0);
        recyclerView.getItemAnimator().setRemoveDuration(0);
        ((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

但是我用了没有效果,该闪烁依旧闪烁,当刷新的时候View是会复用的,闪烁时,后面的元素复用了前面元素的ViewHolder。

找到一篇博文,提供了一个解决方法

  @Override
        public void onBindViewHolder(TestDBViewHolder holder, int position) {
            holder.binding.setData(list.get(position));
            holder.binding.executePendingBindings();//加一行,问题解决
        }   

加了一行代码解决了ViewHolder复用闪烁的问题,但是executePendingBindings()这个方法从未用过,跟下去源码中的注释如下

   /**
     * Evaluates the pending bindings, updating any Views that have expressions bound to
     * modified variables. This <b>must</b> be run on the UI thread.
     */
    public void executePendingBindings() {
        if (mContainingBinding == null) {
            executeBindingsInternal();
        } else {
            mContainingBinding.executePendingBindings();
        }
    }

我的理解是,这个方法是的意思是当数据还没有被绑定加载的时候,executePendingBindings,它使数据绑定刷新所有挂起的更改。否则,它将视为另一个布局失效了。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值