-1

I have 1 RV for load 23 data question, but I want to divide it into 4 data sections, for example, A = 8 data, B = 5 data, C = 5 data, and D = 5 data.

I have AdapterQuestion like this :

public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.HolderData> {

private List<Question> listQuestion;
private Context context;

private LayoutInflater inflter;
public static ArrayList<String> kdPertanyaan;
public static ArrayList<String> kdKuesioner;
public static ArrayList<String> selectedAnswers;


public QuestionAdapter(Context context, List<Question> listQuestion) {
    this.context = context;
    this.listQuestion = listQuestion;
    kdPertanyaan = new ArrayList<>();
    for (int i = 0; i < listQuestion.size(); i++) {
        kdPertanyaan.add("Nilai tidak boleh kosong");
    }
    kdKuesioner = new ArrayList<>();
    for (int i = 0; i < listQuestion.size(); i++) {
        kdKuesioner.add("Nilai tidak boleh kosong");
    }
    // initialize arraylist and add static string for all the questions
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < listQuestion.size(); i++) {
        selectedAnswers.add("Jawaban tidak boleh kosong");
    }

    inflter = (LayoutInflater.from(context));
}

public void setListQuestion(List<Question> listQuestion) {
    this.listQuestion = listQuestion;
}

@NonNull
@Override
public HolderData onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_pertanyaan,parent, false);
    HolderData holder = new HolderData(layout);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull HolderData holder, @SuppressLint("RecyclerView") final int position) {
    Question question = listQuestion.get(position);
    holder.txtKdPertanyaan.setText(kdPertanyaan.set(position, question.getKd_pertanyaan()));
    holder.txtKdKuesioner.setText(kdKuesioner.set(position, question.getKd_kuesioner()));
    holder.txtNo.setText(question.getNo());
    holder.txtPertanyaan.setText(question.getPertanyaan());
    holder.question = question;
    holder.rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(position, "1");
        }
    });

    holder.rb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(position, "3");
        }
    });

    holder.rb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(position, "5");
        }
    });

    holder.rb7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(position, "7");
        }
    });
}

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

class HolderData extends  RecyclerView.ViewHolder{
    @BindView(R.id.kd_pertanyaan) TextView txtKdPertanyaan;
    @BindView(R.id.kd_kuesioner) TextView txtKdKuesioner;
    @BindView(R.id.no) TextView txtNo;
    @BindView(R.id.question) TextView txtPertanyaan;
    @BindView(R.id.rbValueOf1) RadioButton rb1;
    @BindView(R.id.rbValueOf3) RadioButton rb3;
    @BindView(R.id.rbValueOf5) RadioButton rb5;
    @BindView(R.id.rbValueOf7) RadioButton rb7;

    Question question;
    HolderData(View v) {
        super(v);
        ButterKnife.bind(this, v);
    }
}

}

how do I change my adapter so that I can implement the recyclerview section with 4 categories A, B, C, D as the title without using a library and only use 1 RV only, is that impossible to do?

Arthur Attout
  • 2,701
  • 2
  • 26
  • 49
R Rifa Fauzi Komara
  • 1,915
  • 6
  • 27
  • 54

1 Answers1

1

With RecyclerView it is possible to have different ViewHolder. Here is an example how to do it.

For your case, i saw it's like a quiz for user. isn't it? Probably, you need to have TabLayout with ViewPager. Inside ViewPager Fragment you could show list of questions. User could swipe between sections. TabLayout is for swiching of section and also shows title of section. Here is an example.

Dmytro Ivanov
  • 1,260
  • 1
  • 8
  • 10