RadioButton with Custom View Android

后端 未结 1 859
小鲜肉
小鲜肉 2021-01-13 16:03

I\'m trying to implement a radiobuttons with custom views like the below screenshot.

The count of the radio button will be dynamic. I tried to create a

1条回答
  •  清酒与你
    2021-01-13 16:29

    You can do it with Custom Listview.

    activity_list_view.xml

    
    
        
    
    

    Custom list item: radio_button_item.xml

    
    
        
    
        
    
            
    
            
        
    
        
    
    

    Now use a custom adapter to add content from Activity:

    public class RadioButtonActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_view);
    
            //     RadioGroup  radioGroup  = (RadioGroup) findViewById(R.id.radioGroup);
            final List saveItems = new ArrayList<>();
    
            String code = null, condition = null, valid = null;
    
            // save your values here to the ItemContent List. usign dummy values as of now
            for (int i = 1; i < 6; i++) {
                code = "CodeXYZ000" + i;
                condition = "Purchase More than Rs." + i + "00";
                valid = "Valid until Dec 30";
                saveItems.add(itemContents(i, code, condition, valid));
            }
            final CustomAdapter adapter = new CustomAdapter(this, R.layout.activity_list_view, saveItems);
            ListView listView = (ListView) findViewById(R.id.radioGroup);
            listView.setAdapter(adapter);
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    adapter.setSelectedIndex(position);  // set selected position and notify the adapter
                    adapter.notifyDataSetChanged();
                }
            });
        }
    
        private static ItemContents itemContents(int i, final String code, final String condition, final String valid) {
    
            ItemContents itemContent = new ItemContents();
            itemContent.setId(i);
            itemContent.setmCode(code);
            itemContent.setmCondition(condition);
            itemContent.setmValid(valid);
            return itemContent;
    
        }
    }
    

    And the Custom Adapter:

    public class CustomAdapter extends ArrayAdapter {
        int selectedIndex = -1;
    
        public CustomAdapter(Context context, int activity_radio_button, List saveItems) {
            super(context, activity_radio_button, saveItems);
        }
    
        public void setSelectedIndex(int index) {
            selectedIndex = index;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View v = convertView;
    
            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.radio_button_item, null);
            }
    
            RadioButton rbSelect = (RadioButton) v
                    .findViewById(R.id.radioButton);
            if(selectedIndex == position){  // check the position to update correct radio button.
                rbSelect.setChecked(true);
            }
            else{
                rbSelect.setChecked(false);
            }
    
            ItemContents itemContents = getItem(position);
    
            if (itemContents != null) {
                TextView textCode = (TextView) v.findViewById(R.id.textCode);
                TextView textCond = (TextView) v.findViewById(R.id.textCond);
                TextView textValid = (TextView) v.findViewById(R.id.textValid);
    
    
                textCode.setText(itemContents.getmCode());
                textCond.setText(itemContents.getmCondition());
                textValid.setText(itemContents.getmValid());
            }
    
            return v;
        }
    }
    

    And your output(I worked this on tv so the big screen)

    [Output[1]

    0 讨论(0)
提交回复
热议问题