//简单优化之后的
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();