//简单优化之后的

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);}@Overrideprotected 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;// 存储每一行所有的childViewList<View> lineViews = new ArrayList<View>();int cCount = getChildCount();// 遍历所有的孩子for (int i = 0; i < cCount; i++){//获取子控件View child = getChildAt(i);//子控件的点击事件child.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {//必须把view强转成一个新控件,不然一直都是最后一个itemTextView 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保存下一行的childViewmAllViews.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++){// 每一行的所有的viewslineViews = mAllViews.get(i);// 当前行的最大高度lineHeight = mLineHeight.get(i);// 遍历当前行所有的Viewfor (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,bottomint 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;}}@Overrideprotected 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的lpMarginLayoutParams 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*/@Overridepublic LayoutParams generateLayoutParams(AttributeSet attrs) {// TODO Auto-generated method stubreturn 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);}@Overridepublic ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs){return new MarginLayoutParams(getContext(), attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);measureChildren(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected 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() {@Overridepublic 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);}

FlowLayout 流式布局加点击事件相关推荐

  1. 自定义 FlowLayout流式布局搜索框 加 GreenDao存取搜索记录,使用RecyclerView展示

    输入框布局的shape <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android ...

  2. Android FlowLayout 流式布局

    FlowLayout 流式布局 Android 流式布局控件,实现自动换行,操出范围可以滑动功能,未使用控件复用功能,所以不应该有太多的子控件. 主要包含功能: 流式布局,自动换行 使用Adapter ...

  3. Flowlayout流式布局使用(轻量级)

    Flowlayout属于自定义流式布局,意思就是说从左上角开始添加原件,依次往后排,第一行挤满了就换一行接着排. 本文所使用的FlowLayout来自于鸿洋大神的框架. 只取了一个自定义控件,没有鸿洋 ...

  4. android 搜索历史流布局,FlowLayout流式布局实现搜索清空历史记录

    本文实例为大家分享了FlowLayout实现搜索清空历史记录的具体代码,供大家参考,具体内容如下 效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中 ...

  5. FlowLayout流式布局实现搜索清空历史记录

    效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中,每次进入页面都先判断sp里是否有值并展示 首先需要导入一个module,下载地址:https:/ ...

  6. Android FlowLayout流式布局

    最近使用APP的时候经常看到有 这种流式布局 ,今天我就跟大家一起来动手撸一个这种自定义控件. 首先说一下自定义控件的流程: 自定义控件一般要么继承View要么继承ViewGroup View的自定义 ...

  7. 4.布局:FlowLayout流式布局(Java swing 入门)

    FlowLayout(流式布局管理器)是 JPanel 和 JApplet 的默认布局管理器.FlowLayout 会将组件按照从上到下.从左到右的放置规律逐行进行定位.与其他布局管理器不同的是,Fl ...

  8. Android 实现FlowLayout流式布局(热门标签)

    先上效果图: 接着看代码实现: public class FlowLayout extends ViewGroup {protected DataSetObserver mDataSetObserve ...

  9. FlowLayout流式布局管理器与网格布局GridLayout

    总结 1.继承JFrame类 2.在最上方定义组件 3.在构造方法中创建组件 4.在构造方法中添加组件 5.设置窗体属性 6.显示窗体 7.在主函数中创建对象 所有布局管理器都可以添加任意组件 (滚动 ...

最新文章

  1. 深入剖析Redis系列(七) - Redis数据结构之列表
  2. jQuery 表格插件
  3. 罗永浩直播带货花落谁家?不止是价高者得之
  4. 技术人的少年感,和年龄无关。
  5. java实现泛型检索_高级Java泛型:检索泛型类型参数
  6. apache代理IIS的80端口实现共存
  7. dp主机_HDMI和DP谁才是未来主流?
  8. Bootstrap 排版正文
  9. [ZZ] HD7970GE vs GTX770
  10. centos host在哪 local_centos怎么查看hostid
  11. C语言编程练习:猜数游戏
  12. 一张纸厚度是多少毫米_一张A4纸的厚度是多少mm?
  13. UnityEditor代码分享导出材质贴图和Mesh本体
  14. 数字货币&区块链动态
  15. Qt主线程和工作线程更新界面问题
  16. alpine linux 简介
  17. MATLAB 线性规划
  18. C++primer 第五版 练习题【3.32】 P104页 个人解答
  19. STM32 FreeRTOS (三) 软件定时器
  20. 聚类算法(五)——层次聚类 linkage (含代码)

热门文章

  1. 蓝桥杯 Python 练习题 数列排序
  2. Python-修改图片分辨率
  3. 通俗易懂理解几何光学(六)光学系统的像质评价
  4. 一枚钻戒如何成功借势世界杯,与粉丝秀恩爱
  5. 深入解析Apache NIFI的调度策略
  6. 数据结构之树的相关名字解释
  7. c语言编写while乘法表,用C语言的while循环,打印九九乘法表,
  8. 电脑预装Office2016打开Word时点击保存弹出“word无法启动转换器RECOVR32.CNV”对话框问题的修复方法
  9. 基于JSP(java)网络百宝箱的设计和实现(含源文件)
  10. raid5硬盘插到计算机读取,【哭着写下这篇文章】win2008软raid5变成2个磁盘组的数据恢复...