···x
public class MyLiuShiView extends ViewGroup {
private int color;
private final static int MYLEFT=20;
private final static int MYTOP=20;
private FlowClick flowClick;
public MyLiuShiView(Context context) {
super(context);
}
public MyLiuShiView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
public MyLiuShiView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.flow);
color = typedArray.getColor(R.styleable.flow_textColor, 0);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//累加宽度变量
int mywidth = MYLEFT;
//设置行高变量
int myheight = MYTOP;
for (int i = 0; i <getChildCount() ; i++) {
//获取每个元素
final TextView childAt = (TextView) getChildAt(i);
//长按删除
childAt.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
post(new Runnable() {
@Override
public void run() {
Toast.makeText(getContext(),"删除了",Toast.LENGTH_SHORT).show();
removeView(v);
}
});
return false;
}
});
childAt.setTextColor(color);
childAt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
flowClick.FlowItemClick((TextView) v);
}
});
//获取标签宽度
int childAtWidth = childAt.getMeasuredWidth();
int childAtHeight = childAt.getMeasuredHeight();
if (MYLEFT+childAtWidth+mywidth>getWidth()){
mywidth=MYLEFT;
myheight+=MYTOP+childAtHeight;
childAt.layout(mywidth,myheight,mywidth+childAtWidth,myheight+childAtHeight);
}else {
childAt.layout(mywidth,myheight,mywidth+childAtWidth,myheight+childAtHeight);
}
mywidth+=MYLEFT+childAtWidth;
}
}
public void setFlowItemClick(FlowClick flowClick){
this.flowClick=flowClick;
}
public void MyAddView(String s) {
TextView text = (TextView)View.inflate(getContext(), R.layout.myliushi_layout, null);
text.setText(s);
addView(text);
}
public interface FlowClick{
void FlowItemClick(TextView view);
}
}