首页 > 解决方案 > 在 recyclerview 适配器项目的第 0 个索引位置上单击它还选择第 7 个索引位置,然后像这样选择第 14 个索引位置。下面是代码片段

问题描述

下面是我实现了 recyclerview 的 QuestionActivity 类的代码。

问题陈述

当我单击 recyclerview 适配器的第一个元素时,它还会选择适配器的第 7 个位置,但该值未插入或从第 7 个元素传递。值只是从第一个元素而不是从第 7 个元素传递。当我单击第 2 个元素时,它还会选择适配器中的第 8 个元素,并且还选择第 16 个元素,这件事正在发生。我不知道该怎么做,因为我只是在适配器中选择一个元素,但它选择了多个元素,选择第 7 个元素背后的逻辑是什么。

活动课

  QuestionPaperAdapter questionPaperAdapter = new QuestionPaperAdapter(QuestionPaperActivity.this, response.body());
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(QuestionPaperActivity.this, LinearLayoutManager.HORIZONTAL, false);
                    SnapHelper snapHelper = new PagerSnapHelper();
                    snapHelper.attachToRecyclerView(questionPaperRecyclerView);
                    questionPaperRecyclerView.setLayoutManager(linearLayoutManager);
                    questionPaperRecyclerView.setAdapter(questionPaperAdapter);

适配器类

它是完整的适配器类

public class QuestionPaperAdapter extends RecyclerView.Adapter<QuestionPaperAdapter.QuestionVH> {
    private static final String TAG = "QuestionPaperAdapter";
    private QuestionPaperActivity context;
    private StartTestQuestionResModel startTestDetailsModel;
    private String mediaBase = "https://amazonaws.com/media/";


    private List<StartTestQuestionListModel> startTestQuestionListModels;

    public QuestionPaperAdapter(QuestionPaperActivity context, StartTestQuestionResModel startTestDetailsModel) {
        this.context = context;
        this.startTestDetailsModel = startTestDetailsModel;

    }

    @NonNull
    @Override
    public QuestionVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.question_paper_parts_fullpage, parent, false);

        startTestQuestionListModels = new ArrayList<>();
        return new QuestionVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final QuestionVH holder, final int position) {
if (startTestDetailsModel.getResult().get(position).getType().equals("question")) {

            holder.subjectCardOne.setVisibility(View.GONE);
            Log.d(TAG, "position in question -->> " + String.valueOf(position));

            holder.questionCard.setVisibility(View.VISIBLE);
            holder.subjectNameTV.setText(startTestDetailsModel.getResult().get(position).getSubject());

            holder.questoinNumberTV.setText(String.valueOf(startTestDetailsModel.getResult().get(position).getQuestionNo()));
            holder.questoinTV.setText(startTestDetailsModel.getResult().get(position).getQuestion());
            holder.answerOneTV.setText(startTestDetailsModel.getResult().get(position).getA());
            holder.answerTwoTV.setText(startTestDetailsModel.getResult().get(position).getB());
            holder.answerThreeTV.setText(startTestDetailsModel.getResult().get(position).getC());
            holder.answerFourTV.setText(startTestDetailsModel.getResult().get(position).getD());



        holder.answerOneLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d(TAG, "position in question one -->> " + String.valueOf(position));

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                        holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                        holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                        holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    }


            }
        });




        holder.answerTwoLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                    holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                }
            }
        });


        holder.answerThreeLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                    holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                }
            }
        });


        holder.answerFourLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                }
            }
        });


    }



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

    public class QuestionVH extends RecyclerView.ViewHolder {
        private TextView subjectNameTVOne, questoinNumberTV, subjectNameTVTwo, subjectNameTVThree, subjectNameTVFour, questoinTV, answerOneTV, answerTwoTV, answerThreeTV, answerFourTV;
        private TextView answerOneRB, answerTwoRB, answerThreeRB, answerFourRB, instructionTV, subjectNameTV;
        private CardView subjectCardOne, subjectCardTwo, subjectCardThree, subjectCardFour, questionCard;
        private RecyclerView partOneRecyclerView, partTwoRecyclerView, partThreeRecyclerView, partFourRecyclerView;
        private LinearLayout answerOneLL, answerTwoLL, answerThreeLL, answerFourLL;
        private MathView mathview;
        private ImageView instructionImg, questoinImg, answerOneImg, answerTwoImg, answerThreeImg, answerFourImg;

        public QuestionVH(View itemView) {
            super(itemView);

            mathview = itemView.findViewById(R.id.mathview);
            subjectNameTV = itemView.findViewById(R.id.subjectNameTV);

            subjectCardOne = itemView.findViewById(R.id.subjectCardOne);
            instructionTV = itemView.findViewById(R.id.instructionTV);
            subjectNameTVOne = itemView.findViewById(R.id.subjectNameTVOne);

            answerTwoLL = itemView.findViewById(R.id.answerTwoLL);
            answerOneLL = itemView.findViewById(R.id.answerOneLL);
            answerThreeLL = itemView.findViewById(R.id.answerThreeLL);
            answerFourLL = itemView.findViewById(R.id.answerFourLL);


            questoinTV = itemView.findViewById(R.id.questoinTV);
            questoinNumberTV = itemView.findViewById(R.id.questoinNumberTV);
            questionCard = itemView.findViewById(R.id.questionCard);

            answerOneTV = itemView.findViewById(R.id.answerOneTV);
            answerTwoTV = itemView.findViewById(R.id.answerTwoTV);
            answerThreeTV = itemView.findViewById(R.id.answerThreeTV);
            answerFourTV = itemView.findViewById(R.id.answerFourTV);

            answerOneRB = itemView.findViewById(R.id.answerOneRB);
            answerTwoRB = itemView.findViewById(R.id.answerTwoRB);
            answerThreeRB = itemView.findViewById(R.id.answerThreeRB);
            answerFourRB = itemView.findViewById(R.id.answerFourRB);


        }
    }

}

如果您对上述代码有任何疑问,请发表评论并告诉我。

标签: androidandroid-recyclerview

解决方案


好的,所以问题是由于 RecyclerView 回收视图持有者而发生的。

现在您的 onClickListener 将在您的查看器上设置内容,但发生的情况是,当它被回收时,它不会再次被调用,因此旧布局仍然显示为您的案例位置 7 等。

解决方案:

在 bind viewholder 中有一个 switch case 检查选择的内容:

在您的问题模型中添加一个变量,并添加它的 getter setter 方法

private int answerClicked;

现在执行点击时,将当前问题模型的 answerClicked 变量设置为点击的答案

    holder.answerOneLL.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d(TAG, "position in question one -->> " + String.valueOf(position));

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    startTestDetailsModel.getResult().get(position).setAnswerClicked(1);
                    holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                    holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));

                    notifyDataSetChanged();  //facepalm for me forgetting this
                }


        }
    });




    holder.answerTwoLL.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                startTestDetailsModel.getResult().get(position).setAnswerClicked(2);
                holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));

              notifyDataSetChanged();
            }
        }
    });


    holder.answerThreeLL.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                startTestDetailsModel.getResult().get(position).setAnswerClicked(3);
                holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));

              notifyDataSetChanged();
            }
        }
    });


    holder.answerFourLL.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                startTestDetailsModel.getResult().get(position).setAnswerClicked(4);
                holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));

             notifyDataSetChanged();
            }
        }
    });

现在将以下内容添加到您的 Bind Viewholder 方法中

  if(startTestDetailsModel.getResult().get(position).getType().equals("question") && startTestDetailsModel.getResult().get(position).getAnswerClicked()==1){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                    holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                    holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                }
  }else if(startTestDetailsModel.getResult().get(position).getType().equals("question") && startTestDetailsModel.getResult().get(position).getAnswerClicked()==2){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
            }
  }else if(startTestDetailsModel.getResult().get(position).getType().equals("question") && startTestDetailsModel.getResult().get(position).getAnswerClicked()==3){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
                holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
            }
  }else if(startTestDetailsModel.getResult().get(position).getType().equals("question") && startTestDetailsModel.getResult().get(position).getAnswerClicked()==4){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                holder.answerOneRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerTwoRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerThreeRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle));
                holder.answerFourRB.setBackground(context.getDrawable(R.drawable.round_answer_cricle_solid));
            }
  }

推荐阅读