下面的代码是一个recyclerview适配器,如果对radiogroup有疑问,则适配器不会保存单选按钮状态并且滚动太慢,
有什么建议吗? onClick
函数由活动实现,以存储使用选择并计算一些业务。
public class PersonalityTestAdapter extends RecyclerView.Adapter<PersonalityTestAdapter.ViewHolder> {
private List<Question> questions = new ArrayList<>();
private List<Integer> choises = new ArrayList<>();
private final PersonalityTestAdapter.OnItemClickListner listner;
private Context context;
public interface OnItemClickListner{
void onItemClick(int id,int checkedId);
}
@NonNull
@Override
public PersonalityTestAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.personality_test_question_item,viewGroup,false);
return new PersonalityTestAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PersonalityTestAdapter.ViewHolder viewHolder, int i) {
viewHolder.bind(i,listner);
}
@Override
public int getItemCount() {
if(questions == null){
return 0;
}else{
return questions.size();
}
}
public PersonalityTestAdapter(OnItemClickListner listener){
this.listner = listener;
}
public void setData(List<Question> questions, List<Integer> choises){
this.questions = questions;
this.choises = choises;
notifyDataSetChanged();
}
class ViewHolder extends RecyclerView.ViewHolder{
@BindView(R.id.personality_test_question_id)
TextView mQuestionId;
@BindView(R.id.personality_test_question)
TextView mQuestion;
@BindView(R.id.personality_test_radio_group)
RadioGroup mRadioGroup;
public ViewHolder(View itemView){
super(itemView);
ButterKnife.bind(this,itemView);
}
public void bind(final int id, final OnItemClickListner listner){
Question question = questions.get(id);
if(question != null){
mQuestionId.setText(String.valueOf(question.getId()));
itemView.
Log.d("id", String.valueOf(question.getId()));
mQuestion.setText(question.getQuestion());
Integer checkedId = choises.get(id);
if(checkedId == -1){
mRadioGroup.clearCheck();
}else{
mRadioGroup.check(checkedId);
}
}
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
listner.onItemClick(id,checkedId);
choises.add(id,checkedId);
}
});
//itemView.setOnClickListener(new View.OnClickListener() {
// @Override
//public void onClick(View v) {
//listner.onItemClick(id);
//Log.d("firdt"," this was clicked"+id);
//}
//});
}
}
}