android - 我可以在没有库部分 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 部分,这是不可能的吗?
解决方案
使用 RecyclerView 可以有不同的 ViewHolder。这是一个如何做到这一点的例子。
对于您的情况,我认为这就像对用户的测验。不是吗?可能,您需要有 TabLayout 和 ViewPager。在 ViewPager 片段中,您可以显示问题列表。用户可以在部分之间滑动。TabLayout 用于切换部分,还显示部分的标题。这是一个例子。
推荐阅读
- javascript - 使用 Javascript 对 cookie 进行排序
- r - 返回两个字符 '.' 之间的字符串
- python - 无法通过 Selenium 截取正确的屏幕截图
- c# - 使用 Dynamics GP 用户名和密码的 SqlConnection -- 用户登录失败
- django - Django API 返回 404 错误,但只有 30% 的时间。如何识别 Docker 设置中的错误?
- php - 按父级对 Eloquent 集合进行排序
- javascript - Node.js createReadStream 错误处理
- mysql - 编译 facebook/mysql 时出现 CMake 错误,链接到奇怪的路径
- python - 如何从python字典中的键值对中获取同一键的所有值
- ios - 在 Swift 中远程通知后使相机灯闪烁