gpt4 book ai didi

android - 使用 RecyclerView 上的多个单选按钮的单选按钮

转载 作者:行者123 更新时间:2023-12-05 00:21:01 27 4
gpt4 key购买 nike

 @Override
public void onBindViewHolder(@NonNull StateViewHolder stateViewHolder, int i) {
GetStateApiResponse getStateApiResponse = state.get(i);
stateViewHolder.btnstate.setText(getStateApiResponse.getStateName().toString());
stateViewHolder.disttCount.setText(getStateApiResponse.getCount().toString());

stateViewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton checked_rb = group.findViewById(checkedId);

if (lastChecked != null) {
lastChecked.setChecked(false);
}
//store the clicked radiobutton
lastChecked = checked_rb;
}
});

}

如果我单击单选按钮而不是自动取消选中,则此代码在 recyclerview 上无法正常工作

最佳答案

这个问题的正确答案

private RadioButton lastChecked
...
@Override
public void onBindViewHolder(@NonNull StateViewHolder stateViewHolder, int i) {
GetStateApiResponse getStateApiResponse = state.get(i);
stateViewHolder.btnstate.setText(getStateApiResponse.getStateName().toString());
stateViewHolder.disttCount.setText(getStateApiResponse.getCount().toString());


if (lastChecked!=null && lastCheckedPos!=-1 && i==lastCheckedPos)
{
lastChecked.setChecked(true);
}
else
{
stateViewHolder.btnstate.setChecked(false);
}



stateViewHolder.btnstate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

for(GetStateApiResponse tempModel: state)
tempModel.setSetchecked(false);

state.get(i).setSetchecked(true);
lastCheckedPos=i;

if(null != lastChecked && !v.equals(lastChecked))
lastChecked.setChecked(false);
lastChecked = (RadioButton) v;
lastChecked.setChecked(true);


}
});


}

关于android - 使用 RecyclerView 上的多个单选按钮的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54689765/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com