在RecyclerView

时间:2017-10-27 09:47:47

标签: android checkbox android-recyclerview radio-button spinner

我有一个RecyclerView,它有四种类型的视图

  1. 的EditText
  2. 单选按钮
  3. 复选框
  4. 微调
  5. 当我滚动RecyclerView并填充了一些内容(如选择一个单选按钮)时,随着我的视图再次重复,该值也会在下一个视图中重复出现。

    我的适配器代码位于

    之下
      

    ExerciseTestAdapter

        public class ExerciseTestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
        private List<String> mList;
        private Context mContext;
    
        private final int FIRST_TYPE = 0;
        private final int SECOND_TYPE = 1;
        private final int THIRD_TYPE = 2;
        private final int FOURTH_TYPE = 3;
    
        public ExerciseTestAdapter(Context mContext) {
            mList = new ArrayList<>();
            this.mContext = mContext;    
        }
    
        public void addAllItems(List<String> items) {
            mList.addAll(items);
            notifyDataSetChanged();
        }
    
        public void deleteAll() {
            mList.clear();
            notifyDataSetChanged();
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
            RecyclerView.ViewHolder holder;
            View v;
            Context context = parent.getContext();
    
    
            switch (viewType) {
                case FIRST_TYPE:
                    v = LayoutInflater.from(context).inflate(R.layout.item_question_text, parent, false);
                    holder = new QuestionEditTextViewHolder(v);
                    break;
    
                case SECOND_TYPE:
                    v = LayoutInflater.from(context).inflate(R.layout.item_question_spinner, parent, false);
                    holder = new QuestionSpinnerViewHolder(v);
                    break;
    
                case THIRD_TYPE:
                    v = LayoutInflater.from(context).inflate(R.layout.item_question_radio, parent, false);
                    holder = new QuestionRadioViewHolder(v);
                    break;
    
                case FOURTH_TYPE:
                    v = LayoutInflater.from(context).inflate(R.layout.item_question_chk_box, parent, false);
                    holder = new QuestionCheckBoxViewHolder(v);
                    break;
    
                default:
                    holder = null;
                    break;
            }
    
            return holder;
        }
    
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            try {
                switch (holder.getItemViewType()) {
                    case FIRST_TYPE:
                        QuestionEditTextViewHolder vh1 = (QuestionEditTextViewHolder) holder;
                        vh1.mQuestionNumber.setText(String.valueOf(position));
    
                        break;
    
                    case SECOND_TYPE:
                        QuestionSpinnerViewHolder vh2 = (QuestionSpinnerViewHolder) holder;
                        vh2.mQuestionNumber.setText(String.valueOf(position));
                        break;
    
                    case THIRD_TYPE:
                        QuestionRadioViewHolder vh3 = (QuestionRadioViewHolder) holder;
                        vh3.mQuestionNumber.setText(String.valueOf(position));
    
                        break;
    
                    case FOURTH_TYPE:
    
                        QuestionCheckBoxViewHolder vh4 = (QuestionCheckBoxViewHolder) holder;
                        vh4.mQuestionNumber.setText(String.valueOf(position));
    
                        break;
    
                    default:
    
                        break;
                }
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public int getItemViewType(int position) {
    
            int SELECTED_TYPE;
    
            switch (mList.get(position)) {
                case "1":
                    SELECTED_TYPE = FIRST_TYPE;
                    break;
    
                case "2":
                    SELECTED_TYPE = SECOND_TYPE;
                    break;
    
                case "3":
                    SELECTED_TYPE = THIRD_TYPE;
                    break;
    
                case "4":
                    SELECTED_TYPE = FOURTH_TYPE;
                    break;
    
                default:
                    SELECTED_TYPE = 100;
                    break;
    
            }
            return SELECTED_TYPE;
        }
    
        @Override
        public int getItemCount() {
            return mList.size();
        }
    
        private class QuestionEditTextViewHolder extends RecyclerView.ViewHolder {
            TextView mQuestionNumber;
    
            QuestionEditTextViewHolder(View v) {
                super(v);
                mQuestionNumber = v.findViewById(R.id.tv_question_num);
            }
        }
    
        private class QuestionSpinnerViewHolder extends RecyclerView.ViewHolder implements AdapterView.OnItemSelectedListener {
    
            private AppCompatSpinner mSpinnerAnswer;
            TextView mQuestionNumber;
    
            QuestionSpinnerViewHolder(View v) {
                super(v);
    
                String[] selectClass = {"Select", "YES", "NO", "OPTION 3", "OPTION 4"};
                mQuestionNumber = v.findViewById(R.id.tv_question_num);
                mSpinnerAnswer = v.findViewById(R.id.spinner_answer);
    
                ArrayAdapter classAdapter = new ArrayAdapter(mContext, android.R.layout.simple_spinner_item, selectClass);
                classAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mSpinnerAnswer.setAdapter(classAdapter);
    
                mSpinnerAnswer.setOnItemSelectedListener(this);
    
            }
    
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
    
            }
        }
    
    
        private class QuestionRadioViewHolder extends RecyclerView.ViewHolder {
    
            TextView mQuestionNumber;
    
            QuestionRadioViewHolder(View v) {
                super(v);
                mQuestionNumber = v.findViewById(R.id.tv_question_num);
            }
        }
    
    
        private class QuestionCheckBoxViewHolder extends RecyclerView.ViewHolder {
    
            TextView mQuestionNumber;
    
            QuestionCheckBoxViewHolder(View v) {
                super(v);
    
                mQuestionNumber = v.findViewById(R.id.tv_question_num);
    
            }
        }
    }
    

    我的活动代码我正在设置我的RecycerView

     mExerciseRecycler = (RecyclerView) findViewById(R.id.exercise_recycler_view);
    
        ExerciseTestAdapter mTopicAdapter = new ExerciseTestAdapter(mContext);
        mExerciseRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mExerciseRecycler.setAdapter(mTopicAdapter);
    
        List<String> mLIst = new ArrayList<>();
    
       //Adding Dummy Data so that there can be many views in Recycler 
        mLIst.add("1");
        mLIst.add("2");
        mLIst.add("3");
        mLIst.add("4");
    
        mLIst.add("3");
        mLIst.add("4");
        mLIst.add("2");
        mLIst.add("1");
    
        mLIst.add("4");
        mLIst.add("3");
        mLIst.add("1");
        mLIst.add("2");
    
        mTopicAdapter.addAllItems(mLIst);
    

    感谢任何帮助。 在此先感谢

1 个答案:

答案 0 :(得分:0)

事情是Recycler视图将重用视图。因此,请确保将所有数据(checkbox,radiobutton,spinner的选择)存储到模型类,并在onBindViewHolder中设置这些值。

创建一个具有视图所有属性的模型类,如:

public class ModelExample {
private String questionNumber;
private String isCheckBoxTicked;
private String isRadioButtonSelected;
private int spinnerPosition;

public String getQuestionNumber() {
    return questionNumber;
}

public void setQuestionNumber(String questionNumber) {
    this.questionNumber = questionNumber;
}

public String getIsCheckBoxTicked() {
    return isCheckBoxTicked;
}

public void setIsCheckBoxTicked(String isCheckBoxTicked) {
    this.isCheckBoxTicked = isCheckBoxTicked;
}

public String getIsRadioButtonSelected() {
    return isRadioButtonSelected;
}

public void setIsRadioButtonSelected(String isRadioButtonSelected) {
    this.isRadioButtonSelected = isRadioButtonSelected;
}

public int getSpinnerPosition() {
    return spinnerPosition;
}

public void setSpinnerPosition(int spinnerPosition) {
    this.spinnerPosition = spinnerPosition;
}
}

还可以使用ModelExample类型的arraylist将数据传递给适配器 并在onBindViewHolder中设置要从模型

查看的所有值
case SECOND_TYPE:
                    QuestionSpinnerViewHolder vh2 = (QuestionSpinnerViewHolder) holder;
                    vh2.mSpinnerAnswer.setSelection(mList.getSpinnerPosition());