FlowLayout 流式布局加点击事件

//简单优化之后的

public class FlowLayout extends ViewGroup {
    private Context con;
    private View child;
    private Dao d;

    public FlowLayout(Context context) {
        this(context,null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        con = context;
        d = new Dao(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mAllViews.clear();
        mLineHeight.clear();
/**
 * 一般是定义为int top;一个top实际上是数组的下标
 left : 指定矩形框左上角的x坐标
 top: 指定矩形框左上角的y坐标
 right: 指定矩形框右下角的x坐标
 bottom:指定矩形框右下角的y坐标
 */
        //获取屏幕的高度和宽度
        int width = getWidth();
        int height = getHeight();
        int lineWidth = 0;
        int lineHeight = 0;
        // 存储每一行所有的childView
        List<View> lineViews = new ArrayList<View>();
        int cCount = getChildCount();
        // 遍历所有的孩子
        for (int i = 0; i < cCount; i++)
        {
            //获取子控件
            View child = getChildAt(i);
            //子控件的点击事件
            child.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    //必须把view强转成一个新控件,不然一直都是最后一个item
                    TextView tv= (TextView) view;
                    String  h= (String) tv.getText();
                    SearchActivity.setEd(h);
                    User u=new User(h,0);
                    d.add(u);
                    //m.addUser(u,0);
                    List<User> users = d.show2();
                    Adaper m=new Adaper(getContext(),users);
                    MainActivity ma=new MainActivity();
                    ma.di(m);
                }
            });

            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值