寫一個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);
}
}
});
三叔2017-06-08 11:04:32
以後這種「動畫」盡量還是用動畫來做吧,
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();