gpt4 book ai didi

java - recyclerview 中的多个倒计时器

转载 作者:行者123 更新时间:2023-12-02 06:04:53 25 4
gpt4 key购买 nike

我的 countdownTimer 工作正常,但我的问题是它们都与最后一个 countdownTimer(recyclerview 中的银行或项目)同步,例如我的时间第一个项目的间隔是 30 秒,第二个项目的间隔是 30 秒,第 3 项的间隔是 60 秒,当我运行我的应用程序时,项目 1 和项目 2 将与项目 3 同步,60 秒,我想我有一个 countdownTimer在整个 recyclerview 中,而不是针对每个项目。

这是我的银行 ListView 持有者:

private static class BankListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private ImageView mImageViewBankLogo;
private TextView mBankName;
private TextView mFirstToken;
private TextView mSecondToken;
private Button mButtonAddToken;
private RelativeLayout mRelativeLayoutFirstTokenRowLayout;
private RelativeLayout mRelativeLayoutSecondTokenRowLayout;
private Callback mCallback;
private ProgressBarView mProgressBarView;
private CountDownTimer countDownTimer;

BankListViewHolder(@NonNull View itemView, Callback callback) {

super(itemView);

mCallback = callback;

mImageViewBankLogo = itemView.findViewById(R.id.imageview_banklist_logo);
mProgressBarView = itemView.findViewById(R.id.imageview_overlay);
mBankName = itemView.findViewById(R.id.textview_banklist_bankname);
mFirstToken = itemView.findViewById(R.id.textview_banklist_firsttoken);
mSecondToken = itemView.findViewById(R.id.textview_banklist_secondtoken);
mButtonAddToken = itemView.findViewById(R.id.button_banklist_addtoken);
mRelativeLayoutSecondTokenRowLayout = itemView.findViewById(R.id.relativelayout_banklist_secondtokenrowlayout);
mRelativeLayoutFirstTokenRowLayout = itemView.findViewById(R.id.relativelayout_banklist_firsttokenrowlayout);
mButtonAddToken.setOnClickListener(this);
mRelativeLayoutSecondTokenRowLayout.setOnClickListener(this);
mRelativeLayoutFirstTokenRowLayout.setOnClickListener(this);
}

@Override
public void onClick(View v) {

if (v.getId() == R.id.relativelayout_banklist_firsttokenrowlayout) {
BankUtil.copyToClipBoard(mFirstToken.getText().toString(), v.getContext());
SnackbarUtil.showSnackbarNotify(v, v.getContext().getString(R.string.banklist_copyotp), v.getContext());
}

if (v.getId() == R.id.relativelayout_banklist_secondtokenrowlayout) {

BankUtil.copyToClipBoard(mSecondToken.getText().toString(), v.getContext());
SnackbarUtil.showSnackbarNotify(v, v.getContext().getString(R.string.banklist_copyotp), v.getContext());
}

if (v.getId() == R.id.button_banklist_addtoken) {
mCallback.navigationToQrScanner(getAdapterPosition());
}
}
}

这是我的onBindViewHolder:

public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

.
.
.
bankListViewHolder.mFirstToken.setText(setSpaceBetweenCharacters(generateOtp(bank, 1)));
bankListViewHolder.mSecondToken.setText(setSpaceBetweenCharacters(generateOtp(bank, 2)));
bankListViewHolder.mButtonAddToken.setVisibility(View.GONE);
bankListViewHolder.mImageViewBankLogo.setImageDrawable(BankUtil.getLogo(mRoot.getContext(), mBankList.get(position)));

int currentSecond = (int) (((System.currentTimeMillis() / 1000) -
(bank.getToken().getOtpGenerationPeriodInSeconds() / 1000)) % (bank.getToken().getOtpGenerationPeriodInSeconds() / 1000));

int diff = ((bank.getToken().getOtpGenerationPeriodInSeconds() / 1000) - currentSecond);


bankListViewHolder.countDownTimer = new CountDownTimer(diff * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {

bankListViewHolder.mProgressBarView.setCounter((int) ((bank.getToken().getOtpGenerationPeriodInSeconds() / 1000) -
(millisUntilFinished / 1000)) * (360 / (bank.getToken().getOtpGenerationPeriodInSeconds() / 1000)));
}

@Override
public void onFinish() {

bankListViewHolder.mFirstToken.setText(generateOtp(bank, 1));
bankListViewHolder.mSecondToken.setText(generateOtp(bank, 2));
mCallback.updateList();
start();

}
};
countDownTimerList.add(bankListViewHolder.countDownTimer);
bankListViewHolder.countDownTimer.start();
mCallback.saveCountDownTimer(bankListViewHolder.countDownTimer);

.
.
.
}

最佳答案

您不应将 countDownTimer 放入 viewHolder 中。请记住,viewHolder 是一个临时 View ,它可以被其他项目重用。这是recyclerView的特性( View 可以回收)。

您应该将 countDownTimer 放入 List 类中。每件一件。或者创建一个新的 countDownTimers ArrayList。

onTick 事件中,您可以使用 notifyItemChanged 更新它们的值。

关于java - recyclerview 中的多个倒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55947755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com