动态地,我在 recycleview 的一个问题下添加了多个选项。现在用户可以在每个问题中选择一个选项。看下图,
Recyclerview Adapter
类是,
@Override
public void onBindViewHolder(final MyViewHolder holder,int position) {
final PollQstWithAns poll = dataList.get(position);
holder.txt_poll_question.setText(poll.getPollQstName());
for (int i = 0; i < poll.getOptionList().size(); i++) {
final PollsData mPollsOptionsList = poll.getOptionList().get(i);
final RadioButton rb = new RadioButton(context);
rb.setText(mPollsOptionsList.getAns1());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled},
new int[]{android.R.attr.state_enabled}
},
new int[]{
Color.DKGRAY
, ContextCompat.getColor(context, R.color.colorPrimary)
}
);
rb.setButtonTintList(colorStateList);
rb.invalidate();
rb.setTextColor(ContextCompat.getColor(context, R.color.gray));
}
holder.optionRadioGroup.addView(rb);
}
}
如果用户选择RadioButton
.RadioGroup