android值radioGroup實現多行多列單選框--動態加入
阿新 • • 發佈:2019-01-29
</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>
由於時間關係就不封裝了,可以自己自定義方式進行封裝