1

我正在构建寻呼机 Recyclerview,其中每个页面都有一个计时器。几秒钟后,我需要根据计时器值更改页面。但问题是,每当我切换到其他页面时,前一个计时器都会再次启动。我做了所有事情,我在 onCreateViewHolder 中创建了计时器,我在 onBindViewHolder 中创建了计时器,就像每个人都说的那样,但没有任何效果。请帮我解决这个问题。下面是我的代码。

 public class GamePlayAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private List<Question> questionList;
    private GamePlayHelper gamePlayHelper;
    private static final int ITEM_TYPE1 = 1;
    private static final int ITEM_TYPE2 = 2;
    private Context context;


    GamePlayAdapter(List<Question> questionList, GamePlayHelper gamePlayHelper, Context context) {
        this.questionList = questionList;
        this.gamePlayHelper = gamePlayHelper;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (questionList.get(position).getMediaExt().equals("")) {
            return ITEM_TYPE1;
        } else {
            return ITEM_TYPE2;
        }
    }

    @NotNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE1) {
            GamePlayItem1Binding gamePlayItemBinding = GamePlayItem1Binding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new GamePlayItem1VH(gamePlayItemBinding);
        } else {
            GamePlayItem2Binding gamePlayItem2Binding = GamePlayItem2Binding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            GamePlayItem2VH gamePlayItem2VH = new GamePlayItem2VH(gamePlayItem2Binding);
            createTimer(gamePlayItem2VH);
            return gamePlayItem2VH;
        }
    }


    private void createTimer(BaseViewHolder viewHolder) {

        if (viewHolder instanceof GamePlayItem1VH) {
            if (((GamePlayItem1VH) viewHolder).countDownTimerUtils != null)
                ((GamePlayItem1VH) viewHolder).countDownTimerUtils.cancel();
            ((GamePlayItem1VH) viewHolder).countDownTimerUtils = new CountDownTimerUtils(10000, 1000, CountDownTimerUtils.TIMER_TYPE.SECONDS) {
                @Override
                public void onTimerTick(String timerValue) {
                    Timber.d(timerValue);
                    ((GamePlayItem1VH) viewHolder).updateTimeValue(timerValue);
                }

                @Override
                public void onTimerFinish() {
                    Timber.d("Finish");
                    ((GamePlayItem1VH) viewHolder).updateScreen();
                    ((GamePlayItem1VH) viewHolder).countDownTimerUtils.cancel();
                }
            };
            ((GamePlayItem1VH) viewHolder).countDownTimerUtils.start();

        } else if (viewHolder instanceof GamePlayItem2VH) {
/*            if (((GamePlayItem2VH) viewHolder).countDownTimerUtils != null)
                ((GamePlayItem2VH) viewHolder).countDownTimerUtils.cancel();*/
            ((GamePlayItem2VH) viewHolder).countDownTimerUtils = new CountDownTimerUtils(10000, 1000, CountDownTimerUtils.TIMER_TYPE.SECONDS) {
                @Override
                public void onTimerTick(String timerValue) {
                    //  Timber.d(timerValue);
                    Log.d("GamePlay", timerValue);
                    ((GamePlayItem2VH) viewHolder).updateTimeValue(timerValue);
                }

                @Override
                public void onTimerFinish() {
                    //Timber.d("Finish");
                    Log.d("GamePlay", "Finish");
                    ((GamePlayItem2VH) viewHolder).updateScreen();
                  //  ((GamePlayItem2VH) viewHolder).countDownTimerUtils.cancel();
                }
            };
            ((GamePlayItem2VH) viewHolder).countDownTimerUtils.start();

        }

    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        holder.onBind(position);
    }

    @Override
    public int getItemCount() {
        return questionList.size();
    }


    class GamePlayItem1VH extends BaseViewHolder {
        private GamePlayItem1Binding binding;
        private int currentPosition = 0;
        private CountDownTimerUtils countDownTimerUtils;


        GamePlayItem1VH(GamePlayItem1Binding binding) {
            super(binding.getRoot());
            this.binding = binding;
            GamePlayItem1ViewModel gamePlayItem1ViewModel = new GamePlayItem1ViewModel();
            binding.setViewModel(gamePlayItem1ViewModel);
            binding.executePendingBindings();
            binding.gameplayItem1QuestionProgress.setMax(questionList.size());
        }

        @Override
        public void onBind(int position) {
            currentPosition = position;
            startTimer();
            updateProgress(position);
            binding.gameplayItem1NoOfQues.setText(String.format("%s/%s", position + 1, questionList.size()));
            binding.gameplayItem1QuestionTitle.setText(questionList.get(position).getQuestionText());
            binding.gameplayItemOptionBtn1.setText(questionList.get(position).getOptions().get(0).getOptionText());
            binding.gameplayItemOptionBtn2.setText(questionList.get(position).getOptions().get(1).getOptionText());

            binding.gameplayItemOptionBtn1.setOnClickListener(v ->
            {
                binding.gameplayItemOptionBtn1.setTextColor(context.getResources().getColor(R.color.white));
                binding.gameplayItemOptionBtn1.setBackgroundResource(R.drawable.button_question_selected_correct);
                onItemClick(position);
            });
            binding.gameplayItemOptionBtn2.setOnClickListener(v ->
            {
                binding.gameplayItemOptionBtn2.setTextColor(context.getResources().getColor(R.color.white));
                binding.gameplayItemOptionBtn2.setBackgroundResource(R.drawable.button_question_selected_wrong);
                onItemClick(position);
            });
            binding.gameplayItemOptionBtn3.setOnClickListener(v ->
            {
                binding.gameplayItemOptionBtn3.setTextColor(context.getResources().getColor(R.color.white));
                binding.gameplayItemOptionBtn3.setBackgroundResource(R.drawable.button_question_selected_correct);
                onItemClick(position);
            });
            binding.gameplayItemOptionBtn4.setOnClickListener(v ->
            {
                binding.gameplayItemOptionBtn4.setTextColor(context.getResources().getColor(R.color.white));
                binding.gameplayItemOptionBtn4.setBackgroundResource(R.drawable.button_question_selected_wrong);
                onItemClick(position);
            });

        }

        private void startTimer() {
            countDownTimerUtils = new CountDownTimerUtils(10000, 1000, CountDownTimerUtils.TIMER_TYPE.SECONDS) {
                @Override
                public void onTimerFinish() {
                    updateScreen();
                    cancel();
                }

                @Override
                public void onTimerTick(String timerValue) {
                    updateTimeValue(timerValue);
                }
            };
            countDownTimerUtils.start();
        }

        private void updateProgress(int position) {
            binding.gameplayItem1QuestionProgress.setProgress(position + 1);
        }


        private void updateTimeValue(String timerValue) {
            binding.gameplayItem1TimerTxt.setText(timerValue);
        }

        private void updateScreen() {
            countDownTimerUtils.cancel();
            onItemClick(currentPosition);
        }

        void onItemClick(int pagePosition) {
            new Handler().postDelayed(() ->
                    gamePlayHelper.onQuestionItemSelected(pagePosition + 1), 500);
        }
    }

    class GamePlayItem2VH extends BaseViewHolder {
        GamePlayItem2Binding binding;
        GamePlayItem2ViewModel gamePlayItem2ViewModel;
        private int currentPosition = 0;
        private CountDownTimerUtils countDownTimerUtils;


        GamePlayItem2VH(GamePlayItem2Binding binding) {
            super(binding.getRoot());
            this.binding = binding;
            gamePlayItem2ViewModel = new GamePlayItem2ViewModel();
            binding.setViewModel(gamePlayItem2ViewModel);
            binding.executePendingBindings();
            binding.gameplayItem2QuestionProgress.setMax(questionList.size());
        }

        @Override
        public void onBind(int position) {
        //    startTimer();
            updateProgress(position);
            currentPosition = position;
            binding.gameplayItem2NoOfQues.setText(String.format("%s/%s", position + 1, questionList.size()));
            binding.gameplayItem2QuestionTitle.setText(questionList.get(position).getQuestionText());
            binding.gameplayItem2OptionBtn1.setText(questionList.get(position).getOptions().get(0).getOptionText());
            binding.gameplayItem2OptionBtn2.setText(questionList.get(position).getOptions().get(1).getOptionText());
            BindingUtils.setImageUrl(binding.gameplayItem2QuestionImg, questionList.get(position).getMediaURL());


            binding.gameplayItem2OptionBtn1.setOnClickListener(v ->
            {
                binding.gameplayItem2OptionBtn1.setTextColor(context.getResources().getColor(R.color.white));
                binding.gameplayItem2OptionBtn1.setBackgroundResource(R.drawable.button_question_selected_correct);
                onItemClick(position);
            });

            binding.gameplayItem2OptionBtn2.setOnClickListener(v ->
            {
                binding.gameplayItem2OptionBtn2.setTextColor(context.getResources().getColor(R.color.white));
                binding.gameplayItem2OptionBtn2.setBackgroundResource(R.drawable.button_question_selected_wrong);
                onItemClick(position);
            });
            binding.gameplayItem2OptionBtn3.setOnClickListener(v ->
            {
                binding.gameplayItem2OptionBtn3.setTextColor(context.getResources().getColor(R.color.white));
                binding.gameplayItem2OptionBtn3.setBackgroundResource(R.drawable.button_question_selected_correct);
                onItemClick(position);
            });
            binding.gameplayItem2OptionBtn4.setOnClickListener(v ->
            {
                binding.gameplayItem2OptionBtn4.setTextColor(context.getResources().getColor(R.color.white));
                binding.gameplayItem2OptionBtn4.setBackgroundResource(R.drawable.button_question_selected_wrong);
                onItemClick(position);
            });
        }

        private void startTimer() {
            countDownTimerUtils = new CountDownTimerUtils(10000, 1000, CountDownTimerUtils.TIMER_TYPE.SECONDS) {
                @Override
                public void onTimerFinish() {
                    updateScreen();
                    cancel();
                    Log.d("GamePlay", "finish");
                }

                @Override
                public void onTimerTick(String timerValue) {
                    updateTimeValue(timerValue);
                    Log.d("GamePlay", timerValue);
                }
            };
            countDownTimerUtils.start();
        }

        private void updateProgress(int position) {
            binding.gameplayItem2QuestionProgress.setProgress(position + 1);
        }

        private void updateScreen() {
//            countDownTimerUtils.cancel();
//            countDownTimerUtils = null;
            gamePlayHelper.onQuestionItemSelected(currentPosition + 1);
        }

        private void updateTimeValue(String timerValue) {
            binding.gameplayItem2TimerTxt.setText(timerValue);
        }

        void onItemClick(int pagePosition) {
//            countDownTimerUtils.cancel();
//            countDownTimerUtils = null;
            new Handler().postDelayed(() ->
                    gamePlayHelper.onQuestionItemSelected(pagePosition + 1), 500);
        }
    }

    void updateDataSet(int position) {
        notifyDataSetChanged();
    }
}
4

0 回答 0