80,350
社区成员
发帖
与我相关
我的任务
分享
package com.meimi.youganjue.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import com.meimi.youganjue.dao.MyPair;
import java.util.ArrayList;
import java.util.Iterator;
public class FlowRadioGroup extends BaseFlowRadioGroup
{
private static final String TAG = "FlowRadioGroup";
public FlowRadioGroup(Context context, AttributeSet attrs)
{
super(context, attrs);
}
private boolean singleSelection;
private String mKey;
public String getKey() {
return mKey;
}
public void setKey(String key) {
this.mKey = key;
keyValues.setFirst(getKey());
}
/**
* 是否单选
* @return
*/
public boolean isSingleSelection() {
return singleSelection;
}
public void setSingleSelection(boolean singleSelection) {
this.singleSelection = singleSelection;
}
MyPair<String,ArrayList> keyValues=new MyPair<>();
{
keyValues.setSecond(new ArrayList<String>());
}
public void setValue(String value){
ArrayList<String> list = keyValues.getSecond();
if(isSingleSelection()){
list.clear();
}else{
}
list.add(value);
}
public ArrayList<String> getValue(){
return keyValues.getSecond();
}
/**
* 正对多选
* @param value
*/
public void removeValue(String value) {
ArrayList<String> list = keyValues.getSecond();
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
String str = iterator.next();
if (str.equalsIgnoreCase(value)) {
Log.i(TAG, "DELETE:" + value);
iterator.remove();
}
}
}
}
这个支持xml的方式包含控件,但是如果是动态添加的依然不行
动态添加的具体代码是;
for (String s : taglist) {
RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.view_radiobutton, tagGroup,false);
radioButton.setText(""+s);
tagGroup.addTagView(radioButton);
}
xml是:
<RadioButton
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/radioBtnStyleTemplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好世界"
>
</RadioButton>
style是:
<style name="radioBtnStyle">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:button">@null</item>
<item name="android:gravity">center_horizontal|center_vertical</item>
<item name="android:textColor">@color/color_btn_selector</item>
</style>
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
int childCount = getChildCount();
int x = 0;
int y = 0;
int row = 0;
for (int index = 0; index < childCount; index++) {
final View child = getChildAt(index);
if (child.getVisibility() != View.GONE) {
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// 此处增加onlayout中的换行判断,用于计算所需的高度
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
x += width;
y = row * height + height;
if (x > maxWidth) {
x = width;
row++;
y = row * height + height;
}
}
}
// 设置容器所需的宽度和高度
setMeasuredDimension(maxWidth, y);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int childCount = getChildCount();
int maxWidth = r - l;
int x = 0;
int y = 0;
int row = 0;
for (int i = 0; i < childCount; i++) {
final View child = this.getChildAt(i);
if (child.getVisibility() != View.GONE) {
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
x += width;
y = row * height + height;
if (x > maxWidth) {
x = width;
row++;
y = row * height + height;
}
child.layout(x - width, y - height, x, y);
}
}
}