对于一个界面里面的选择数据,数据选项的值是动态加载的,并且还有单选和多选,这种情况下如果单独写单选和多选会写很多代码,这里我自己整理了一个工具类可以完美解决动态加载单选和多选数据,并从中获取你选择的值,并且选中的值可以动态显示在界面上。
这里我用到了LinearLayout的线性动态加载布局、流式布局、CheckBox
布局文件:主界面一个垂直方向的线性布局
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
子界面:一个标题,一个动态显示选中数据的TextView,流式布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mianlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:id="@+id/projectLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:orientation="horizontal"">
<TextView
android:id="@+id/rootTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="rootTitle:"
android:textColor="@color/black_text01"
android:textSize="@dimen/sp_17" />
<TextView
android:id="@+id/selected_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@color/white"
android:textColor="@color/black_text03"
android:textSize="@dimen/sp_15"
android:singleLine="true"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray" />
<com.scxd.accidenthandling.widget.FlowLayout
android:id="@+id/flowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="10dp" />
</LinearLayout>
子界面:一个CheckBox
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="17sp"
android:textColor="@color/blac"
android:id="@+id/checkbox"/>
在主界面添加布局,添加数据:
linearLayout.addView(new CustomChckGroup(this, "选项标题", List<T> bean, true, false));
在主界面设置获取到网络数据需要显示出来选中项:
((CustomChckGroup) linearLayout.getChildAt(0)).setCheckData(bean.getT());
在主界面获得数据:
String str = linearLayout.getChildCount() > 0 ? ((CustomChckGroup) linearLayout.getChildAt(0)).getDataTag() : "";
工具类:第一个参数:context,第二个是该选项栏的title,第三个是要显示选项数据的集合,第四个判断是否多选,第五个判断是水平布局还是垂直布局
public class CustomChckGroup extends LinearLayout {
private Context context;
private TextView rootTitleView;
private TextView selectedText;
private FlowLayout mflowLayout;
CheckBox box;
private LinearLayout mianLinear;
private TextView mLine;
private ExpandableLayout expandableLayout;
private LinearLayout proJectLinearLayout;
private ImageView tipImage;
private String strText = "";
/**
* String chckTitle, String chckTag, final int chckSize
* @param mContext
* @param rootTitle
* @param isRadio
* @param isheng
*/
public CustomChckGroup(Context mContext, final String rootTitle, List<T> data, boolean isRadio, boolean isheng) {
super(mContext);
this.context = mContext;
View view = LayoutInflater.from(context).inflate(R.layout.view_customchckgroup, this);
this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
rootTitleView = (TextView) view.findViewById(R.id.rootTitle);
selectedText = (TextView) view.findViewById(R.id.selected_text);
mflowLayout = (FlowLayout) view.findViewById(R.id.flowLayout);
mianLinear = (LinearLayout) view.findViewById(R.id.mianlinear);
mLine = (TextView) view.findViewById(R.id.line);
expandableLayout= (ExpandableLayout) view.findViewById(R.id.expandable_layout);
proJectLinearLayout= (LinearLayout) view.findViewById(R.id.projectLinear);
tipImage= (ImageView) view.findViewById(R.id.project_tip);
if (isheng) {
mianLinear.setOrientation(HORIZONTAL);
mLine.setVisibility(View.GONE);
tipImage.setVisibility(View.GONE);
} else {
mianLinear.setOrientation(VERTICAL);
mLine.setVisibility(View.VISIBLE);
tipImage.setVisibility(View.VISIBLE);
proJectLinearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (expandableLayout.isExpanded()) {
expandableLayout.collapse();
tipImage.setImageResource(R.drawable.xia);
} else {
expandableLayout.expand();
tipImage.setImageResource(R.drawable.shang);
}
}
});
}
rootTitleView.setText(rootTitle);
for (DictionaryBean diction : data) {
box = (CheckBox) LayoutInflater.from(context).inflate(R.layout.flowlayout_children, mflowLayout, false);
box.setText(diction.getDmmc());
box.setTag(diction.getDmz());
mflowLayout.addView(box);
if (isRadio) {
box.setButtonDrawable(getResources().getDrawable(R.drawable.ic_base_selected));
box.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j < mflowLayout.getChildCount(); j++) {
if (((CheckBox) mflowLayout.getChildAt(j)).isChecked()) {
((CheckBox) mflowLayout.getChildAt(j)).setChecked(false);
}
((CheckBox) v).setChecked(true);
selectedText.setText(((CheckBox) v).getText().toString());
}
}
});
} else {box.setButtonDrawable(getResources().getDrawable(R.drawable.ic_base_selected_duo));
box.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j < mflowLayout.getChildCount(); j++) {
if (((CheckBox) mflowLayout.getChildAt(j)).isChecked()) {
map.put(((CheckBox) mflowLayout.getChildAt(j)).getTag().toString(),((CheckBox) mflowLayout
.getChildAt(j)).getText().toString());
}else {
try {
map.remove(((CheckBox) mflowLayout.getChildAt(j)).getTag().toString());
} catch (Exception e){
}
}
}
strText = "";
for (String values : map.values()){
strText += values;
}
selectedText.setText(strText);
}
});
}
}
}
public static String flag;
private Map<String,String> map=new HashMap<>();
/**
* 得到数据
* @return
*/
public String getDataTag() {
String strTAG = "";
String string;
for (int m = 0; m < mflowLayout.getChildCount(); m++) {
string = ((CheckBox) mflowLayout.getChildAt(m)).isChecked() ? ((CheckBox) mflowLayout.getChildAt(m))
.getText().toString() : "";
if (string != null && !"".equals(string)) {
string = string + ",";
} else {
string = "";
}
strTAG += string;
}
return getSubStringDatas(strTAG);
}
public String getSubStringDatas(String string) {
if (string.length() > 0) {
string = string.substring(0, string.length() - 1);
}
return string;
}
/**
* 设置数据
* @param strTAG
*/
public void setCheckData(String strTAG) {
if (strTAG != null && !"".equals(strTAG)) {
if (strTAG.contains(",")) {
String[] datas = strTAG.split(",");
for (int m = 0; m < mflowLayout.getChildCount(); m++) {
for (int j = 0; j < datas.length; j++) {
if (datas[j].equals(((CheckBox) mflowLayout.getChildAt(m)).getTag().toString())) {
((CheckBox) mflowLayout.getChildAt(m)).setChecked(true);
}
}
}
} else {
for (int m = 0; m < mflowLayout.getChildCount(); m++) {
if (strTAG.equals(((CheckBox) mflowLayout.getChildAt(m)).getTag().toString())) {
((CheckBox) mflowLayout.getChildAt(m)).setChecked(true);
map.put( ((CheckBox) mflowLayout.getChildAt(m)).getTag().toString(), ((CheckBox) mflowLayout
.getChildAt(m)).getText().toString());
}
}
}
}
}
static class ViewHolder {
@Bind(R.id.checkbox)
CheckBox checkbox;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}