首页 > 解决方案 > 我可以在没有库部分 recyclerview 的情况下使用部分 recyclerview 吗?

问题描述

我有 1 个 RV 用于加载 23 个数据问题,但我想将其分为 4 个数据部分,例如,A = 8 个数据、B = 5 个数据、C = 5 个数据和 D = 5 个数据。

我有这样的适配器问题:

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);
    }
}

}

如何更改我的适配器,以便我可以在不使用库且仅使用 1 个 RV 的情况下实现具有 4 个类别 A、B、C、D 作为标题的 recyclerview 部分,这是不可能的吗?

标签: androidandroid-recyclerview

解决方案


使用 RecyclerView 可以有不同的 ViewHolder。这是一个如何做到这一点的例子。

对于您的情况,我认为这就像对用户的测验。不是吗?可能,您需要有 TabLayout 和 ViewPager。在 ViewPager 片段中,您可以显示问题列表。用户可以在部分之间滑动。TabLayout 用于切换部分,还显示部分的标题。这是一个例子


推荐阅读