回收站视图中有多个倒计时计时器

时间:2019-05-02 07:41:54

标签: java android android-recyclerview android-adapter countdowntimer

我的countdownTimers工作正常,但是我的问题是它们都与上一个countdownTimerrecyclerview中的银行或项目)同步,例如,我在项目1中的时间间隔是30秒当我运行我的应用程序项目1时,两个是30秒,项目3中是60秒,项目2将与项目3,60sec同步,我认为我在整个countdownTimer中有一个recyclerview,而不是每个项目

这是我的银行列表视图持有人:

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);

.
.
.
}

1 个答案:

答案 0 :(得分:0)

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

您应该将countDownTimer放在List类中。每个项目一个。或创建一个countDownTimers的新ArrayList。

onTick事件中,您可以使用notifyItemChanged更新其值。