先看效果图吧,

继上一篇《android自定义viewgroup实现等分格子布局》中实现的布局效果,这里稍微有些区别,每个格子的高度不规则,就是传说的瀑布流布局,一般实现这种效果,要么用第三方控件,如果不是加载图片还可以直接写在xml中实现,不过代码会很多的;

下面我重写了viewgroup,实现onMeasure,onLayout方法,动态设置每个布局的高度,这里有一个小的技巧,一般我们自定义的控件,嵌套在scrollview中显示不全,这个问题也纠结我一小会,不过当你打开scrollview的源码,你会发现有一个地方,同时可以理解scrollview中嵌套viewpager,gridview,listview时候会显示不全的问题

下面是自定义viewgroup的全部代码:

package com.allen.view;

import com.allen.mygridlayout.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec; /**
* @package:com.fumei.letao.views
* @author:Allen
* @email:jaylong1302@163.com
* @data:2013年11月26日 下午8:39:51
* @description:瀑布流视图
*/
public class WaterfullLayout extends ViewGroup { final String tag = "balance"; // 列数
int columns = 2;
// 行数
int rows = 0;
// 边距
int margin = 10;
// 子视图数量
int count = 0;
private int mMaxChildWidth = 0;
private int mMaxChildHeight = 0; public WaterfullLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.MyGridLayout);
columns = a.getInteger(R.styleable.MyGridLayout_numColumns, 2);
margin = (int) a.getInteger(R.styleable.MyGridLayout_itemMargin, 2);
}
} public WaterfullLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
// TODO Auto-generated constructor stub
} public WaterfullLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mMaxChildWidth = 0;
mMaxChildHeight = 0; count = getChildCount();
if (count == 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
rows = count % columns == 0 ? count / columns : count / columns + 1;// 行数
int top[] = new int[columns];
for (int i = 0; i < rows; i++) {// 遍历行
for (int j = 0; j < columns; j++) {// 遍历每一行的元素
View child = this.getChildAt(i * columns + j);
if (child == null)
break;
ViewGroup.LayoutParams lp = child.getLayoutParams(); if (child.getVisibility() == GONE) {
continue;
} child.measure(MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
lp.height, MeasureSpec.AT_MOST));
top[j] += lp.height + margin;
mMaxChildWidth = Math.max(mMaxChildWidth,
child.getMeasuredWidth());
} } setMeasuredDimension(resolveSize(mMaxChildWidth, widthMeasureSpec),
resolveSize(getMax(top) + margin, heightMeasureSpec)); } @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int height = b - t;// 布局区域高度
int width = r - l;// 布局区域宽度 if (count == 0)
return; int gridW = (width - margin * (columns + 1)) / columns;// 格子宽度
int gridH = 0;// 格子高度 int left = 0;
int top[] = new int[columns]; for (int i = 0; i < rows; i++) {// 遍历行
for (int j = 0; j < columns; j++) {// 遍历每一行的元素
View child = this.getChildAt(i * columns + j);
if (child == null)
return;
ViewGroup.LayoutParams lp = child.getLayoutParams(); child.measure(MeasureSpec.makeMeasureSpec(gridW,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
lp.height, MeasureSpec.AT_MOST)); // 如果最后有一个对其的标志,为了底部对其
if (child.getTag() != null && child.getTag().equals(tag)) {
child.measure(MeasureSpec.makeMeasureSpec(gridW,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
getMax(top) - top[j], MeasureSpec.EXACTLY));
gridH = getMax(top) - top[j];
left = j * gridW + margin * (j + 1);
child.layout(left, top[j] + margin, left + gridW, top[j]
+ gridH);
break;
} gridH = lp.height;
left = j * gridW + margin * (j + 1);
top[j] += margin;
child.layout(left, top[j], left + gridW, top[j] + gridH);
top[j] += gridH;
} }
} /** 计算整体布局高度,为了在嵌套在scrollview中能显示出来 */
private int getMax(int array[]) {
int max = array[0];
for (int i = 0; i < array.length; i++) {
if (max < array[i])
max = array[i];
}
return max;
} }

所有demo工程点击这里下载:click me!!!

 

android自定义viewgroup之我也玩瀑布流的更多相关文章

  1. Android自定义ViewGroup

    视图分类就两类,View和ViewGroup.ViewGroup是View的子类,ViewGroup可以包含所有的View(包括ViewGroup),View只能自我描绘,不能包含其他View. 然而 ...

  2. Android自定义ViewGroup,实现自动换行

    学习<Android开发艺术探索>中自定义ViewGroup章节 自定义ViewGroup总结的知识点 一.自定义ViewGroup中,onMeasure理解 onMeasure(int ...

  3. android自定义viewgroup实现等分格子布局

    先上效果图: 实现这样的效果: 一般的思路就是,直接写布局文件,用LinearLayout 嵌套多层子LinearLayout,然后根据权重layout_weight可以达到上面的效果 还有就是利用g ...

  4. android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu

    示意图就不展示了,和上一节的一样,滑动菜单SlidingMenu效果如何大家都比较熟悉,在这里我简单说明一下用自定义ViewGroup来实现. 实现方法:我们自定义一个ViewGroup实现左右滑动, ...

  5. android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu[转]

    http://blog.csdn.net/jj120522/article/details/8095852 示意图就不展示了,和上一节的一样,滑动菜单SlidingMenu效果如何大家都比较熟悉,在这 ...

  6. Android自定义ViewGroup(四、打造自己的布局容器)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51500304 本文出自:[openXu的博客] 目录: 简单实现水平排列效果 自定义Layo ...

  7. android自定义viewgroup初步之一----抽屉菜单

    转载请注明出处 http://blog.csdn.net/wingichoy/article/details/47832151 几天前在慕课网上看到鸿洋老师的 自定义卫星菜单,感觉很有意思,于是看完视 ...

  8. Android 自定义ViewGroup手把手教你实现ArcMenu

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37567907 逛eoe发现这样的UI效果,感觉很不错,后来知道github上有这 ...

  9. Android -- 自定义ViewGroup实现FlowLayout效果

    1,在开发的时候,常在我们的需求中会有这种效果,添加一个商品的一些热门标签,效果图如下: 2,从上面效果可以看得出来,这是一个自定义的ViewGroup,然后实现换行效果,让我们一起来实现一下 自定义 ...

随机推荐

  1. C#设计模式(23)——备忘录模式(Memento Pattern)

    一.引言 在上一篇博文分享了访问者模式,访问者模式的实现是把作用于某种数据结构上的操作封装到访问者中,使得操作和数据结构隔离.而今天要介绍的备忘者模式与命令模式有点相似,不同的是,命令模式保存的是发起 ...

  2. 安装时出现 Runtiem error (at 62:321) SWbem Locator:服务不存在,或已被标记为删除 该怎么解决?

    这是由wmi服务损坏引起的错误 修复WMI服务损坏的批处理程序 将下列代码复制到一个文本文件中,改名为fixwmi.bat,运行即可.需要一段时间,请大家耐心等候. ================= ...

  3. [翻译]-马丁·福勒-page对象

    译者注:这篇文章翻译自马丁·福勒(Martin Flower,对,没错,就是软件教父)官网的一篇文章,原文出处在文底.如果你正在做WEB自动化测试,那么我强烈推荐你看这篇文章.另外透露Martin F ...

  4. 使用ThreadSanitizer线程检查工具

    ThreadSanitizer又叫TSan,是一个检查线程Data Race的C/C++工具.它集成在新版的gcc和clang中,通过编译时加-fsanitize=thread,可以在运行时检测出Da ...

  5. [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装,此篇文章算是一个简单的进阶应用吧!它是在Windows下通过Selenium+Python实现自动访问Firefox和Chrome并实现搜索截图的功能.        [Pyth ...

  6. celery简单应用

    写作背景介绍 在celery简单入门中已经介绍了写作的背景,这篇文章主要是深入介绍celery的使用技巧.在实际的项目中我们需要明确前后台的分界线,因此我们的celery编写的时候就应该是分成前后台两 ...

  7. paip.提升用户体验--提升java的热部署热更新能力

    paip.提升用户体验--提升java的热部署热更新能力 想让java做到php那么好的热部署能力  "fix online"/在线修复吗??直接在服务器上修改源码生效,无需重启应 ...

  8. android定位GPS定位 代码实现

    package com.lx.util;   import android.content.Context; import android.content.SharedPreferences; imp ...

  9. SQL Server 内存中OLTP内部机制概述(三)

    ----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...

  10. ORACLE 监听日志文件太大停止写监听日志引起数据库连接不上问题

    生产库监听日志文件太大(达到4G多),发现oracle停止写监听日志,检查参数log_file,log_directory,log_status 均正常,数据库运行也正常. 经确认确实为监听日志过大引 ...