recyclerview item中的进度条动画

pdog
  • 573

写一个demo 想在recyclerview中 的item 中的进度条有动画效果

就用不断设置setProgress()的方式来写了,可是在实际过程中,会发生动画播放在不正确的item
即使我判断如果item是被复用的话则取消也无能为力。。


SparseIntArray sparseIntArray = new SparseIntArray();
ArrayMap<Integer, Disposable> arrayMap = new ArrayMap<>();

binding.recyclerView.setAdapter(new BaseRecyclerViewAdapter<Integer>(strings) {
    @Override
    public int getLayoutId(int viewType) {
        return R.layout.recycler_item;//item布局 其中有一个横向的进度条空间
    }

    @Override
    protected void convert(VH holder, Integer i, int position) {
  
        int after = sparseIntArray.get(position);        //查看item是否第一次使用
        Disposable disposable1 = arrayMap.get(position);
        if (disposable1 != null) {
            PtrCLog.d("RecyclerViewActivity", "convert: " + "取消订阅");
            disposable1.dispose();
        }

        if (after == i) {
            PtrCLog.d("RecyclerViewActivity", "convert: " + "相同后直接设置");
            holder.setProgress(R.id.progress_bar_financing_item, i);
        } else {
            sparseIntArray.put(position, i);//存
            int castProgress = (i);      //当前得到的
            PtrCLog.d("LoanListAdapter", "progress: " + castProgress);
            PtrCLog.d("LoanListAdapter", "初次生效当前坐标为: " + position + "-----" + "存储的进度为" + castProgress);
            Disposable disposable = Observable
                    .intervalRange(0, castProgress + 1, 0, 30, TimeUnit.MILLISECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(aLong -> {
                                PtrCLog.d("LoanListAdapter", "convert: " + position + "----" + aLong + "");
                                PtrCLog.d("RecyclerViewActivity", "convert: after" + (after == i));
                                holder.setProgress(R.id.progress_bar_financing_item, Integer.parseInt(aLong + ""));
                            }, throwable -> {
                            }
                            , () -> {
                            });
            arrayMap.put(position, disposable);
        }
    }
});
    
回复
阅读 3.9k
1 个回答
✓ 已被采纳

以后这种“动画”尽量还是用动画来做吧,

 ValueAnimator animator = ValueAnimator.ofInt(0, castProgress);
        animator.addUpdateListener(animation -> {
            int value = (int) animation.getAnimatedValue();
            holder.setProgress(R.id.progress_bar_financing_item, value);
        });
        animator.setDuration(2000);
        animator.start();
宣传栏