【问题标题】:Radiogroup in recyclerview incorrect回收站视图中的 Radiogroup 不正确
【发布时间】:2016-08-16 14:42:58
【问题描述】:

我有一个 recyclerview,其中每个列表项都有一个带有 2 个单选按钮的单选组。如何正确存储每个无线电组的状态。这是我的代码。在向上/向下滚动时,状态不正确。谢谢

   public class ITOAdapter extends RecyclerView.Adapter<ITOAdapter.ViewHolder> {
    private Context context;
    private List<String> list;
    private List<Model> answers;
    private Client client;
    private String test;

    public ITOAdapter(Context context, List<String> list, Client client, String test) {
        this.context = context;
        this.list = list;
        this.client = client;
        this.answers = new ArrayList<>();
        this.test = test;
        for (int i=0; i<list.size(); i++) this.answers.add(new Model());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View contactView = null;
        if (viewType == Constants.VIEW_TYPE_CELL) {
            contactView = inflater.inflate(R.layout.item_ito, parent, false);
        }
        else {
            contactView = inflater.inflate(R.layout.item_ito_footer, parent, false);
        }
        return new ViewHolder(contactView);
    }

    @Override
    public int getItemViewType(int position) {
        return (position == list.size()-1) ? Constants.VIEW_TYPE_FOOTER : Constants.VIEW_TYPE_CELL;
    }


    private void setRadio(final ViewHolder holder, int selection) {

        System.out.println("SELECT:" + selection);
        RadioButton b1 = holder.rb0;
        RadioButton b2 = holder.rb1;

        b1.setChecked(false);
        b2.setChecked(false);

        if (selection == 0) b1.setChecked(true);
        if (selection == 1) b2.setChecked(true);
    }


    @Override
    public void onBindViewHolder(final ViewHolder vh, final int position) {
        vh.pos = position;
        vh.number.setText(vh.con + (position+1));
        vh.question.setText(list.get(position));

        setRadio(vh, answers.get(position).getState());

        vh.rb_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId != -1 ) {
                    RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId);
                    vh.checkId = checkedId;
                    int checkedIndex = group.indexOfChild(checkedRadioButton);
                    answers.get(vh.pos).setState(checkedIndex);
                    setRadio(vh, answers.get(position).getState());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView number;
        public TextViewPlus question;
        public RadioGroup rb_group;
        public Button btn;
        public String con;
        public int checkId;
        public RadioButton rb0, rb1;

        public int pos;

        public ViewHolder(View itemView) {
            super(itemView);
            number = (TextView) itemView.findViewById(R.id.number);
            question = (TextViewPlus) itemView.findViewById(R.id.question);
            rb0 = (RadioButton) itemView.findViewById(R.id.rb0);
            rb1 = (RadioButton) itemView.findViewById(R.id.rb1);
            rb_group = (RadioGroup) itemView.findViewById(R.id.rb_group);
            btn = (Button) itemView.findViewById(R.id.btn);
            con = number.getText().toString();
        }
    }

    private class Model{
        private int state;

        public Model(){
            this.state = -1;
        }

        public Model(int state){
            this.state = state;
        }

        public int getState() {
            return state;
        }

        public void setState(int state) {
            this.state = state;
        }
    }

}

【问题讨论】:

  • 单选按钮的范围是到列表还是到列表项?
  • 如果我理解正确的话,radiogroup 中有保持位置单选按钮。例如:我们有2个radiobutton,我们点击到第一个,然后保存0 else 1

标签: java android android-recyclerview android-adapter


【解决方案1】:

从 onCheckedChanged 中移除 setRadio。同时删除 b1.setChecked(false);和 b2.setChecked(false);来自 setRadio 让我知道你仍然遇到问题 参考ListView with RadioGroup in each row

if (checkedId != -1 ) {
                    RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId);
                    vh.checkId = checkedId;
switch(checkId)
{
case R.id.rb0:
answers.get(vh.pos).setState(0);
break;
case R.id.rb1:
answers.get(vh.pos).setState(1);
break;
}
}

【讨论】:

  • 我做了,但还是有问题(
  • 请检查 int checkedIndex = group.indexOfChild(checkedRadioButton);假设单选组只有 2 个单选按钮作为子项而不是 textview 等,则给出正确的值(0 或 1)。我共享的链接使用 switch case 执行类似的操作。你可以试试我正在更新代码的答案。
猜你喜欢
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 2016-03-20
  • 1970-01-01
相关资源
最近更新 更多