天天看点

android值radioGroup实现多行多列单选框--动态加入

</pre><p></p><p></p><pre code_snippet_id="1552676" snippet_file_name="blog_20160111_2_1960190" name="code" class="java">/**
	 * 意见反馈类型的列表 如 闪退问题、卡顿问题、功能建议
	 * 
	 * 一个radioGroup代表一行,一行有radioCount个radioButton
	 * 
	 * @description
	 * @author zhongwr
	 * @params
	 * @update 2016年1月9日 上午11:48:15
	 */
	private void updateOptionsView(List<FeedbackOptionItem> optionsList) {
		if (!Tools.isListEmpty(optionsList)) {
			llOptionsContainer.removeAllViews();
			int size = optionsList.size();
			final int radioCount = 3;
			int mod = size % radioCount;
			int radioGroupCount = 0 == mod ? size / radioCount : size / radioCount + 1;
			RadioGroup.LayoutParams groupParams = new RadioGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			RadioGroup.LayoutParams radioParams = new RadioGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT, 1);
			RadioGroup radioGroup = null;
			for (int i = 0; i < radioGroupCount; i++) {
				if (i != radioGroupCount - 1) {
					groupParams.setMargins(0, 0, 0, 24);
				}
				radioGroup = new RadioGroup(mContext);
				radioGroup.setLayoutParams(groupParams);
				radioGroup.setOrientation(RadioGroup.HORIZONTAL);
				radioGroup.setWeightSum(radioCount);
				int radioMaxeSize = (i + 1) * radioCount;
				for (int j = i * radioCount; j < radioMaxeSize && j < size; j++) {
					FeedbackOptionItem optionItem = optionsList.get(j);
					optionItem.radioGroupIndex = i;
					RadioButton radio = new RadioButton(this);
					radio.setLayoutParams(radioParams);
					radio.setText(optionItem.title);
					radio.setTag(optionItem);
					radio.setButtonDrawable(R.drawable.feedback_radiobtn_style_selector);
					// 设置图片与文本的间距,据说必须先setButtonDrawable,再调用
					radio.setPadding(14, 0, 0, 0);
					radioGroup.addView(radio);
				}
				radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					/** 只要状态发生改变(从选中到未选中或者从未选中到选中) */
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
						if (null != radioButton && radioButton.isChecked()) {// 选中的item才会执行这个
							selectedOptionItem = (FeedbackOptionItem) radioButton.getTag();
							// 首次选中不需要重置各个radioGroup的状态
							if (null != selectedOptionItem && -1 != selectedRadioGroupIndex
									&& selectedRadioGroupIndex != selectedOptionItem.radioGroupIndex) {
								resetUnselectedRadioGroupStatus(selectedOptionItem.radioGroupIndex);
							}
							selectedRadioGroupIndex = selectedOptionItem.radioGroupIndex;
						}
					}


				});
				llOptionsContainer.addView(radioGroup);
			}
		}
	}

	/***
	 * 
	 * @description 恢复未选中的radioGroup状态
	 * @author zhongwr
	 * @params radioGroupIndex 被选中的radioGroup的下标
	 * @update 2016年1月9日 下午2:44:06
	 */
	private void resetUnselectedRadioGroupStatus(int radioGroupIndex) {
		if (null != llOptionsContainer) {
			// int size = radioGroupList.size();
			int size = llOptionsContainer.getChildCount();
			Logcat.dLog("size = " + size);
			for (int i = 0; i < size; i++) {
				if (i == radioGroupIndex) {
					continue;
				}
				View view = llOptionsContainer.getChildAt(i);
				if (null != view && view instanceof RadioGroup) {
					((RadioGroup) view).clearCheck();
				}
				// RadioGroup radioGroup = radioGroupList.get(i);
				// radioGroup.clearCheck();
			}
		}
	}

/***
 * 意见反馈:信息详情item
 * 
 * @author "zhongwr"
 * 
 */
public class FeedbackOptionItem {
	/** 类型id */
	public String tid;

	public String title;
	/**本地赋值: 当前item所属的radioGroup的下标 */
	public int radioGroupIndex;
}
           

llOptionsContainer的布局如下: 

<LinearLayout

                    android:id="@+id/ll_feedback_options_parent"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:layout_marginTop="12dp"

                    android:background="@color/gray_f"

                    android:orientation="vertical" >

                </LinearLayout>

radioButton 的样式selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/fuli_choose_grey" android:state_checked="false"/>

    <item android:drawable="@drawable/fuli_choose_red" android:state_checked="true"/>

</selector>

由于时间关系就不封装了,可以自己自定义方式进行封装