I have a RecyclerView which have four type of Views
- EditText
- Radio Button
- CheckBox
- Spinner
When I Scroll the RecyclerView with Some content filled (like Selecting a Radio Button) then the value is getting duplicated in the next views also, as my views are repeating again.
My Adapter Code is below
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);
}
}
}
My Activity Code where I am setting my 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);
Any help is appreciated. Thanks in Advance
Aucun commentaire:
Enregistrer un commentaire