簡體   English   中英

Recyclerview中的Radiogroup不正確

[英]Radiogroup in recyclerview incorrect

我有一個recyclerview,每個列表項都有一個帶2個單選按鈕的radiogroup。 如何正確存儲每個放射組的狀態。 這是我的代碼。 在向上/向下滾動時,狀態是不相關的。 謝謝

   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;
        }
    }

}

從onCheckedChanged中刪除setRadio。 同時刪除b1.setChecked(false); 和b2.setChecked(false); 來自setRadio,讓我知道你仍然遇到問題參考ListView與每一行中的RadioGroup

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;
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM