我正在以编程方式向我的recyclerview添加radiogroups,并且它工作得很好。但是当我检查它并滚动recyclerview时,它会失去选中的单选按钮。
我已经看到了许多解决方案的方法和示例,但我无法实现它。已经连续几天了。
如下所示,我将选中的单选按钮保存在模型中。
适配器:
我已经看到了许多解决方案的方法和示例,但我无法实现它。已经连续几天了。
如下所示,我将选中的单选按钮保存在模型中。
适配器:
@Override
public void onBindViewHolder(final NROptionLineHolder holder, int position) {
holder.priceGroup.removeAllViews();
holder.priceGroup.setOnCheckedChangeListener(null);
int id = (position+1)*100;
checklistModel = mChecklists.get(position);
holder.packageName.setText(checklistModel.getTitle());
for(String price : checklistModel.getQuestions()){
RadioButton rb = new RadioButton(NROptionLineAdapter.this.context);
rb.setId(id++);
rb.setText(price);
holder.priceGroup.addView(rb);
}
holder.priceGroup.check(checklistModel.getSelectedId());
holder.priceGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
checklistModel.setSelectedId(checkedId);
Log.d(TAG, "onCheckedChanged: " + checkedId);
}
});
}
持有人
OnNROptionListener onNROptionListener;
public NROptionLineHolder(View itemView, OnNROptionListener onNROptionListener) {
super(itemView);
packageName = itemView.findViewById(R.id.package_name);
priceGroup = itemView.findViewById(R.id.price_grp);
// priceGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(RadioGroup radioGroup, int i) {
//
// Log.d(TAG, "onCheckedChanged: " + radioGroup.getCheckedRadioButtonId() + " " + i);
// }
// });
this.onNROptionListener = onNROptionListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onNROptionListener.onNROptionClick(getAdapterPosition());
}
public interface OnNROptionListener {
void onNROptionClick(int position);
}
}
编辑1 - 单选按钮组
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/package_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="@+id/price_grp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/package_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:orientation="horizontal"/>
</android.support.constraint.ConstraintLayout>
编辑 2
根据要求,以下是来自我的 ChecklistActivity 的重要代码:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist);
intent = getIntent();
size = intent.getIntExtra("size", 0);
nr = intent.getIntExtra("nr", 0);
Log.d(TAG, "Checklist Activity - Qtd Questões: " + size);
Log.d(TAG, "Checklist Activity - NR: " + nr);
btnSaveCheck = findViewById(R.id.btnSaveChecklist);
mRecyclerView = findViewById(R.id.package_lst);
setupRecycler();
btnSaveCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Sucesso", Toast.LENGTH_SHORT).show();
}
});
}
private void setupRecycler() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
setupList();
mAdapter = new NROptionLineAdapter(data, this, getApplication());
mRecyclerView.setAdapter(mAdapter);
}
private void setupList(){
data = new ArrayList<>();
class setupList extends AsyncTask<Void, Void, List<MRNrOption>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<MRNrOption> doInBackground(Void... voids) {
list = DatabaseClient
.getInstance(getApplicationContext())
.getAppDatabase()
.mrNrOptionDAO()
.loadAllByNRId(nr);
return list;
}
@Override
protected void onPostExecute(List<MRNrOption> list) {
super.onPostExecute(list);
List<String> priceList = new ArrayList<>();
priceList.add("Sim");
priceList.add("Não");
priceList.add("Não se Aplica");
for (int i=0; i<list.size(); i++) {
Log.d(TAG, "NRs Activity - Adding To List: " + list.get(i).getTitle());
data.add(new Checklist(
list.get(i).getTitle(),
priceList)
);
mAdapter.notifyDataSetChanged();
}
}
}
setupList lm = new setupList();
lm.execute();
}
编辑3 - 重要
由于我从服务器获取所有问题,用户之前的选择会影响问题数量,所以RadioGroups和RadioButtons是以编程方式生成的,这就是我需要这样做的原因。
编辑4
编辑5 - Checklist模型类
public class Checklist {
String title;
List<String> questions;
boolean isRadioButtonAdded;
int selectedId;
public Checklist(String title, List<String> questions) {
this.title = title;
this.questions = questions;
}
public Checklist(){}
public boolean getIsAdded(){
return isRadioButtonAdded;
}
public void setIsAdded(boolean isAdded){
this.isRadioButtonAdded = isAdded;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getQuestions() {
return questions;
}
public void setQuestions(List<String> questions) {
this.questions = questions;
}
public int getSelectedId() {
return selectedId;
}
public void setSelectedId(int selectedId) {
this.selectedId = selectedId;
}
}