android值radioGroup实现多行多列单选框--动态加入
2016-01-09 17:04
483 查看
</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>
由于时间关系就不封装了,可以自己自定义方式进行封装
相关文章推荐
- 酷派大神9976A刷机
- android之旅26 JNI概念与运用
- AndroidManifest.xml
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
- Android Studio debug使用release的签名
- Android 中的 Intent 简介
- android JNI开发准备
- 详解:android- monkey命令
- android ndk环境配置
- Android - AlertDialog
- Android 自己定义ViewGroup 实战篇 -> 实现FlowLayout
- Android 使用APICloude 数据库的方法
- Ubuntu 14.04 搭建MTK Android源码编译环境搭建
- Android 失去焦点,关闭弹出的软键盘
- 适配器模式在Android中的应用
- 【已解决】新建Android Studio项目,卡死了
- Android源代码仓库及其管理工具Repo分析
- Android 仿今日头条频道管理(下)(GridView之间Item的移动和拖拽)
- 给 Android 开发者的 RxJava 详解