Xc_流式布局

···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);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值