1,自定义flowlayout代码

package com.hyang.administrator.studentproject.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup; import java.util.ArrayList;
import java.util.List; /**
* Created by Administrator on 2017/6/20.
*/
public class FlowGroupView extends ViewGroup {
/**
* 储存所有的view 按行记录
*/
private List<List<View>> mAllViews = new ArrayList<List<View>>();
/**
* 记录每一行的高度
*/
private List<Integer> mLineHeight = new ArrayList<Integer>();
private String TAG = "TAG"; public FlowGroupView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
} public FlowGroupView(Context context, AttributeSet attrs) {
super(context, attrs);
} public FlowGroupView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 置空 view 容器 和 lineHeight 容器 重新赋值
//因为OnMeasure方法会走两次,第一次是实例化这个对象的时候高度和宽度都是0
//之后走了OnSizeChange()方法后 又走了一次OnMeasure,所以要把第一次加进去的数据清空。
mAllViews.clear();
mLineHeight.clear();
//得到上级容器为其推荐的宽高和计算模式
int specWidthMode = MeasureSpec.getMode(widthMeasureSpec);
int specHeighMode = MeasureSpec.getMode(heightMeasureSpec);
int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);
int specHeighSize = MeasureSpec.getSize(heightMeasureSpec);
// 计算出所有的 child 的 宽和高
// measureChildren(specWidthSize, specHeighSize);
// 记录如果是 warp_content 是设置的宽和高
int width = 0;
int height = 0;
// 得到子view的个数
int cCount = getChildCount();
/**
* 记录每一行的宽度,width不断取最大宽度
*/
int lineWidth = 0;
/**
* 每一行的高度,累加至height
*/
int lineHeight = 0; // 存储每一行所有的childView
List<View> lineViews = new ArrayList<View>(); for (int i = 0; i < cCount; i++) {
// 得到每个子View
View child = getChildAt(i);
// 测量每个子View的宽高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 当前子view的lp
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
// 子view的宽和高
int cWidth = 0;
int cheight = 0;
// 当前子 view 实际占的宽
cWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
// 当前子View 实际占的高
cheight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
lineHeight=cheight;
// 需要换行
if(lineWidth + cWidth > specWidthSize){
width = Math.max(lineWidth, cWidth);// 取最大值
lineWidth = cWidth; // 开启新行的时候重新累加width
// 开启新行时累加 height
// lineHeight = cheight; // 记录下一行的高度
mAllViews.add(lineViews);
mLineHeight.add(cheight);
lineViews = new ArrayList<>();
// 换行的时候把该 view 放进 集合里
lineViews.add(child);// 这个 view(child) 是下一行的第一个view
height += cheight; //每个View高度是一样的,直接累加
Log.e("需要换行", "hight--" + height);
Log.e("onMeasure", "AllViews.size() -- > " + mAllViews.size());
}else {
// 不需要换行
lineWidth += cWidth;//
Log.e("不需要换行","hight--"+height);
// 不需要换行时 把子View add 进集合
lineViews.add(child);
} if(i == cCount-1){
// 如果是最后一个view
width = Math.max(lineWidth, cWidth);
height += cheight;
Log.e("最后一个view","hight--"+height);
}
}
// 循环结束后 把最后一行内容add进集合中
mLineHeight.add(lineHeight); // 记录最后一行
mAllViews.add(lineViews);
// MeasureSpec.EXACTLY 表示设置了精确的值
// 如果 mode 是 MeasureSpec.EXACTLY 时候,则不是 warp_content 用计算来的值,否则则用上级布局分给它的值
setMeasuredDimension(
specWidthMode == MeasureSpec.EXACTLY ? specWidthSize : width,
specHeighMode == MeasureSpec.EXACTLY ? specHeighSize : height
);
Log.e("onMeasure", "mAllViews.size() -- > " + mAllViews.size() + " mLineHeight.size() -- > " + mLineHeight.size() + "Height -- > "+height);
} /**
* 所有childView的位置的布局
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 当前行的最大高度
int lineHeight = 0;
// 存储每一行所有的childView
List<View> lineViews = new ArrayList<View>();
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); Log.e("onLayout" , "第" + i + "行 :" + lineViews.size()+"-------lineHeight"+ lineHeight); // 遍历当前行所有的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;
}
Log.v("onLayout", "onLayout mAllViews.size() -- > " + mAllViews.size() + " mLineHeight.size() -- > "+ mLineHeight.size());
} /**
* 这个一定要设置,否则会包强转错误
* 设置它支持 marginLayoutParams
*/
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(),attrs);
}
}

2.在布局文件中使用

 <com.hyang.administrator.studentproject.widget.FlowGroupView
android:id="@+id/flow_view_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.hyang.administrator.studentproject.widget.FlowGroupView>

3.TextView的样式文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#E7E7E7" >
</solid>
<corners
android:radius="30dp"
/> <padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>

4.在Activity中使用

 package com.hyang.administrator.studentproject.activity;

 import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; import com.hyang.administrator.studentproject.R;
import com.hyang.administrator.studentproject.widget.FlowGroupView; import org.xutils.view.annotation.ViewInject;
import org.xutils.x; import java.util.ArrayList; public class FlowLayoutActivity extends AppCompatActivity { @ViewInject(R.id.flow_button)
private Button addTextButton;
@ViewInject(R.id.flow_view_group)
private FlowGroupView flowView; private ArrayList<String> names; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flow_layout);
x.view().inject(this); setTwoFlowLayout(); addTextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addTextView("添加1");
}
});
} private void setTwoFlowLayout() {
//添加数据
names = new ArrayList<String>();
names.add("降龙十八掌");
names.add("黯然销魂掌");
names.add("左右互搏术");
names.add("七十二路空明拳");
names.add("小无相功");
names.add("拈花指");
names.add("打狗棍法");
names.add("蛤蟆功");
names.add("九阴白骨爪");
names.add("一招半式闯江湖");
names.add("醉拳");
names.add("龙蛇虎豹");
names.add("葵花宝典");
names.add("吸星大法");
names.add("如来神掌警示牌");
//为布局添加内容
for (int i = 0; i < names.size(); i++) {
addTextView(names.get(i));
}
} /**
* 动态添加布局
* @param str
*/
private void addTextView(String str) {
TextView child = new TextView(this);
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
child.setLayoutParams(params);
child.setBackgroundResource(R.drawable.flag);
child.setText(str);
child.setTextColor(Color.WHITE);
initEvents(child);//监听
flowView.addView(child);
} /**
* 为每个view 添加点击事件
*/
private void initEvents(final TextView tv){
tv.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(FlowLayoutActivity.this, tv.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}

Android流式布局控件的更多相关文章

  1. Android流式布局实现

    查看我的所有开源项目[开源实验室] 欢迎增加我的QQ群:[201055521],本博客client下载[请点击] 摘要 新项目用到了一种全新布局----Android标签流式布局的功能,正好一直说给大 ...

  2. Kotlin 第一弹:自定义 ViewGroup 实现流式标签控件

    古人学问无遗力, 少壮工夫老始成.纸上得来终觉浅, 绝知此事要躬行. – 陆游 <冬夜读书示子聿> 上周 Google I/O 大会的召开,宣布了 Kotlin 语言正式成为了官方开发语言 ...

  3. android流式布局、待办事项应用、贝塞尔曲线、MVP+Rxjava+Retrofit、艺术图片应用等源码

    Android精选源码 android模仿淘宝首页效果源码 一款艺术图片应用,采用T-MVVM打造 Android MVP + RxJava + Retrofit项目 android流式布局实现热门标 ...

  4. android -------- 流式布局,支持单选、多选等

    最近开发中有流式标签这个功能,网上学了下,来分享一下 Android 流式布局,支持单选.多选等,适合用于产品标签等. 效果图: 用法: dependencies { compile 'com.hym ...

  5. 含有过滤功能的android流式布局

    FilterFlowLayout 含有过滤功能的流式布局, 參考FlowLayout 能够去除宽度不在范围(比例或真实值)内的子view 能够设置最大行数 能够加入组件间水平间距 能够加入行间距 系统 ...

  6. Android控件进阶-自定义流式布局和热门标签控件

    技术:Android+java   概述 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧,类 ...

  7. 【Android - 自定义View】之自定义可滚动的流式布局

    首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 FlowLayout ,继承自ViewGroup类: (2)在这个自定义View中,用户可以放入所有继承自View类的视图,这个 ...

  8. 【Android】7.1 布局控件常用的公共属性

    分类:C#.Android.VS2015: 创建日期:2016-02-10 一.简介 Android应用程序中的布局控件都是容器控件,用于控制子元素的排列和放置方式.Android提供的布局控件有: ...

  9. Android开发之基本控件和详解四种布局方式

    Android中的控件的使用方式和iOS中控件的使用方式基本相同,都是事件驱动.给控件添加事件也有接口回调和委托代理的方式.今天这篇博客就总结一下Android中常用的基本控件以及布局方式.说到布局方 ...

随机推荐

  1. golang-Tag

    Tag 理解 Golang中可以对struct定义Tag 例如: type TestTag struct{ UserName string `json:"name"` Age In ...

  2. vue2高仿饿了么app

    Github地址: https://github.com/ccyinghua/appEleme-project 一.构建项目所用: vue init webpack appEleme-project ...

  3. CSS&JS定位器

    一.CssSelector定位器 1.概述 CssSelector是效率很高的元素定位方法,Selenium官网的Document里极力推荐使用CSS locator,而不是XPath来定位元素,原因 ...

  4. Python语法糖

    1.装饰器 ####装饰器的固定格式 ##普通版本 def timer(func): def inner(*args,**kwargs): '''执行函数之前要做的''' ret = func(*ar ...

  5. SQL里的concat() 以及group_concat() 函数的使用

    实例参考:https://blog.csdn.net/mary19920410/article/details/76545053 一 concat()函数 1.功能:将多个字符串连接成一个字符串. 2 ...

  6. ios应用数据存储方式(归档) - 转

    一.简单说明  1.在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦.  2.偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置 ...

  7. SPOJ PRIME1 - Prime Generator(线性筛)

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  8. ABAP术语-Distribution Model

    Distribution Model 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/25/1052434.html Model that d ...

  9. ubuntu以root进入图形化界面

    sudo nautilus 可以进行一些文件夹移动操作,不会出现权限的问题

  10. CSS3--j惊艳到你的新前端

    一.css3的选择器 1. 父子选择器 直接关系 .box>.com 2. 兄弟选择器 相邻关系 .box+.com <span>hello</span> <p&g ...