Set selected index of an Android RadioGroup

前端 未结 7 1050
予麋鹿
予麋鹿 2021-02-01 11:52

Is there a way to set the selected index of a RadioGroup in android, other than looping through the child radiobuttons and selecting checking the radio button at the selected in

7条回答
  •  后悔当初
    2021-02-01 12:19

    Inside onBindViewHolder set the tag to Button Group

    @Override
    public void onBindViewHolder(final CustomViewHolder holder, final int position) {
       ...
       holder.ButtonGroup.setTag(position);
    }
    

    and in the ViewHolder

    ButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
            ...
            int id = radioGroup.getCheckedRadioButtonId();
    
            RadioButton radioButton = (RadioButton) radioGroup.findViewById(id);
    
            int clickedPos = (Integer) radioGroup.getTag();
    
            packageModelList.get(clickedPos).getPoll_quest()
        }
    

提交回复
热议问题