您的位置:首页 > 其它

FlowLayout 流式布局加点击事件

2018-01-01 21:32 351 查看
//简单优化之后的

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();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();

// 如果已经需要换行
if (childWidth + lp.leftMargin + lp.rightMargin + lineWidth > width)
{
// 记录这一行所有的View以及最大高度
mLineHeight.add(lineHeight);
// 将当前行的childView保存,然后开启新的ArrayList保存下一行的childView
mAllViews.add(lineViews);
lineWidth = 0;// 重置行宽
lineViews = new ArrayList<View>();
}
/**
* 如果不需要换行,则累加
*/
lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
+ lp.bottomMargin);
lineViews.add(child);
}
// 记录最后一行
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);

int left = 0;
int top = 0;
// 得到总行数
int lineNums = mAllViews.size();
for (int i = 0; i < lineNums; i++)
{
// 每一行的所有的views
lineViews = mAllViews.get(i);
// 当前行的最大高度
lineHeight = mLineHeight.get(i);
// 遍历当前行所有的View
for (int j = 0; j < lineViews.size(); j++)
{
View child = lineViews.get(j);
if (child.getVisibility() == View.GONE)
{
continue;
}
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();

//计算childView的left,top,right,bottom
int lc = left + lp.leftMargin;
int tc = top + lp.topMargin;
int rc =lc + child.getMeasuredWidth();
int bc = tc + child.getMeasuredHeight();
child.layout(lc, tc, rc, bc);

left += child.getMeasuredWidth() + lp.rightMargin
+ lp.leftMargin;
}
left = 0;
top += lineHeight;
}

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 获得它的父容器为它设置的测量模式和大小
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

Log.e(TAG, sizeWidth + "," + sizeHeight);

// 如果是warp_content情况下,记录宽和高
int width = 0;
int height = 0;
/**
* 记录每一行的宽度,width不断取最大宽度
*/
int lineWidth = 0;
/**
* 每一行的高度,累加至height
*/
int lineHeight = 0;

int cCount = getChildCount();

// 遍历每个子元素
for (int i = 0; i < cCount; i++)
{
child = getChildAt(i);
// 测量每一个child的宽和高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 得到child的lp
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();
// 当前子空间实际占据的宽度
int childWidth = child.getMeasuredWidth() + lp.leftMargin
+ lp.rightMargin;
// 当前子空间实际占据的高度
int childHeight = child.getMeasuredHeight() + lp.topMargin
+ lp.bottomMargin;
/**
* 如果加入当前child,则超出最大宽度,则的到目前最大宽度给width,类加height 然后开启新行
*/
if (lineWidth + childWidth > sizeWidth)
{
width = Math.max(lineWidth, childWidth);// 取最大的
lineWidth = childWidth; // 重新开启新行,开始记录
// 叠加当前高度,
height += lineHeight;
// 开启记录下一行的高度
lineHeight = childHeight;
} else
// 否则累加值lineWidth,lineHeight取最大高度
{
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
// 如果是最后一个,则将当前记录的最大宽度和当前lineWidth做比较
if (i == cCount - 1)
{
width = Math.max(width, lineWidth);
height += lineHeight;
}

}
setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth
: width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight
: height);

}
/**
* 存储所有的View,按行记录
*/
private List<List<View>> mAllViews = new ArrayList<List<View>>();
/**
* 记录每一行的最大高度
*/
private List<Integer> mLineHeight = new ArrayList<Integer>();

/**
* 与当前ViewGroup对应的LayoutParams
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
// TODO Auto-generated method stub

return new MarginLayoutParams(getContext(), attrs);
}
}
//没有优化的
public class MyGroupView extends ViewGroup {
Button btn;
View view;
AttributeSet attributeSet;
public MyGroupView(Context context) {
this(context,null);
}

public MyGroupView(Context context, AttributeSet attrs) {
super(context, attrs,0);
}

public MyGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
{
return new MarginLayoutParams(getContext(), attrs);
}

@Override
protected v
4000
oid onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);

}

@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
/**
* 一般是定义为int top;一个top实际上是数组的下标
left : 指定矩形框左上角的x坐标
top: 指定矩形框左上角的y坐标
right: 指定矩形框右下角的x坐标
bottom:指定矩形框右下角的y坐标
*/
int width = getWidth();
int height = getHeight();
int tw = 0;
int th = 0;
for (int ii = 0; ii < getChildCount(); ii++) {
View child = getChildAt(ii);
view = child;
btn = (Button) view;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Dao dao = new Dao(getContext());
Button bt = (Button) view;
String a = bt.getText().toString();
Users users = new Users(a,"新品","特别厚","挨冻");
dao.ins(users);
MainActivity.aa();
MyTitle.ss(a);
}
});

if (tw + child.getWidth() < width) {

} else {
tw = 0;
th += child.getMeasuredHeight();   //超过屏幕的宽度,自动换行
}

child.layout(tw, th, tw + child.getMeasuredWidth(), th + child.getMeasuredHeight());
tw += child.getMeasuredWidth();
}
}

}

//赋值

fl= findViewById(R.id.fl);
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.leftMargin = 5;
lp.rightMargin = 5;
lp.topMargin = 5;
lp.bottomMargin = 5;

for(int i = 0; i < list.size(); i ++){
TextView view = new TextView(this);
view.setText(list.get(i));
view.setTextColor(Color.BLACK);
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
fl.addView(view,lp);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: