前面在学习鸿洋大神的一些自定义的view文章,看到了自定义viewgroup实现浮动标签,初步看了下他的思路以及结合自己的思路完成了自己的浮动标签的自定义viewgroup。目前实现的可以动态添加标签、可点击。效果图如下:
1、思路
首先在onmeasure方法中测量viewgroup的宽和高,重点是处理当我们自定义的viewgroup设置为wrap_content的情况下,如何去测量其大小的问题。当我们自定义的viewgroup设置为wrap_content时,我们需要让子view先去测量自己,当子view测量完后,再通过子view的getmeasuredwidth和getmeasureheight方法获得其子view的宽和高。每次在测量一个子view之前,都需要判断如果加入该子view,当前行是否能够容纳下该子view,如果不能,则需要新开一行,并记录下当前行的最大高度。
在onlayout方法中,核心人物是给每个子view摆放位置,也就是为该viewgroup中每个子view找到盒子模型上面的两个点也就是左上角和右下角,即点(l,t)和点(r,b),确定了两个点,子view的位置也就确定了。
2、实现
基本思路有了就可以尝试实现了,代码如下:
自定义的viewgroup:
/**
* 流式标签(动态的,根据传入的数据动态添加标签)
*/
public class dynamictagflowlayout extends viewgroup {
private list<string> mtags = new arraylist<string>();
public dynamictagflowlayout(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
}
public dynamictagflowlayout(context context, attributeset attrs) {
super(context, attrs);
}
public dynamictagflowlayout(context context) {
super(context);
}
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
int widthmode = measurespec.getmode(widthmeasurespec);
int widthsize = measurespec.getsize(widthmeasurespec);
int heightmode = measurespec.getmode(heightmeasurespec);
int heightsize = measurespec.getsize(heightmeasurespec);
//当前viewgroup的总高度
int totalheight= 0;
//所有行中的最大宽度
int maxlinewidth = 0;
//当前行的最大高度
int linemaxheight = 0;
//当前行的总宽度
int currentlinewidth = 0;
//每个childview所占用的宽度
int childviewwidthspace = 0;
//每个childview所占用的高度
int childviewheightspace = 0;
int count = getchildcount();
marginlayoutparams layoutparams;
for(int i = 0; i < count; i++){
view child = getchildat(i);
if(child.getvisibility() != view.gone){//只有当这个view能够显示的时候才去测量
//测量每个子view,以获取子view的宽和高
measurechild(child, widthmeasurespec, heightmeasurespec);
layoutparams = (marginlayoutparams) child.getlayoutparams();
childviewwidthspace = child.getmeasuredwidth() + layoutparams.leftmargin + layoutparams.rightmargin;
childviewheightspace = child.getmeasuredheight() + layoutparams.topmargin + layoutparams.bottommargin;
if(currentlinewidth + childviewwidthspace > widthsize){//表示如果当前行再加上现在这个子view,就会超出总的规定宽度,需要另起一行
totalheight += linemaxheight;
if(maxlinewidth < currentlinewidth){//如果行的最长宽度发生了变化,更新保存的最长宽度
maxlinewidth = currentlinewidth;
}
currentlinewidth = childviewwidthspace;//另起一行后,需要重置当前行宽
linemaxheight = childviewheightspace;
}else{//表示当前行可以继续添加子元素
currentlinewidth += childviewwidthspace;
if(linemaxheight < childviewheightspace){
linemaxheight = childviewheightspace;
}
}
}
}
setmeasureddimension(widthmode == measurespec.exactly ? widthsize : maxlinewidth, heightmode == measurespec.exactly ? heightsize : totalheight);
}
@override
protected void onlayout(boolean changed, int l, int t, int r, int b) {
//当前是第几行
int currentline = 1;
//存放每一行的最大高度
list<integer> linemaxheightlist = new arraylist<integer>();
//每个childview所占用的宽度
int childviewwidthspace = 0;
//每个childview所占用的高度
int childviewheightspace = 0;
//当前行的最大高度
int linemaxheight = 0;
//当前行的总宽度
int currentlinewidth = 0;
int count = getchildcount();
marginlayoutparams layoutparams;
for(int i = 0; i < count; i++){
int cl= 0, ct = 0, cr = 0, cb = 0;
view child = getchildat(i);
if(child.getvisibility() != view.gone){//只有当这个view能够显示的时候才去测量
layoutparams = (marginlayoutparams) child.getlayoutparams();
childviewwidthspace = child.getmeasuredwidth() + layoutparams.leftmargin + layoutparams.rightmargin;
childviewheightspace = child.getmeasuredheight() + layoutparams.topmargin + layoutparams.bottommargin;
system.out.println("getwidth()---->"+getwidth());
if(currentlinewidth + childviewwidthspace > getwidth()){//表示如果当前行再加上现在这个子view,就会超出总的规定宽度,需要另起一行
linemaxheightlist.add(linemaxheight);//此时先将这一行的最大高度加入到集合中
//新的一行,重置一些参数
currentline++;
currentlinewidth = childviewwidthspace;
linemaxheight = childviewheightspace;
cl = layoutparams.leftmargin;
if(currentline > 1){
for(int j = 0; j < currentline - 1; j++){
ct += linemaxheightlist.get(j);
}
ct += layoutparams.topmargin ;
}else{
ct = layoutparams.topmargin;
}
}else{//表示当前行可以继续添加子元素
cl = currentlinewidth + layoutparams.leftmargin;
if(currentline > 1){
for(int j = 0; j < currentline - 1; j++){
ct += linemaxheightlist.get(j);
}
ct += layoutparams.topmargin;
}else{
ct = layoutparams.topmargin;
}
currentlinewidth += childviewwidthspace;
if(linemaxheight < childviewheightspace){
linemaxheight = childviewheightspace;
}
}
cr = cl + child.getmeasuredwidth();
cb = ct + child.getmeasuredheight();
child.layout(cl, ct, cr, cb);
}
}
}
@override
public layoutparams generatelayoutparams(attributeset attrs) {
return new marginlayoutparams(getcontext(), attrs);
}
public void settags(list<string> tags){
if(tags!= null){
mtags.clear();
mtags.addall(tags);
for(int i = 0; i < mtags.size(); i++){
textview tv = new textview(getcontext());
marginlayoutparams lp = new marginlayoutparams(marginlayoutparams.wrap_content, marginlayoutparams.wrap_content);
lp.setmargins(15, 15, 15, 15);
// lp.width = marginlayoutparams.wrap_content;
// lp.height = marginlayoutparams.wrap_content;
tv.setlayoutparams(lp);
tv.setbackgroundresource(r.drawable.tv_bg);
/*
* setpadding一定要在setbackgroundresource后面使用才有效!!!
* http://stackoverflow.com/questions/18327498/setting-padding-for-textview-not-working
*/
tv.setpadding(15, 15, 15, 15);
tv.settextcolor(color.white);
tv.settext(mtags.get(i));
tv.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
if(listener != null){
listener.onclick(v);
}
}
});
addview(tv);
}
requestlayout();
}
}
private ontagitemclicklistener listener;
public interface ontagitemclicklistener{
public void onclick(view v);
}
public void setontagitemclicklistener(ontagitemclicklistener l){
listener = l;
}
}
mainactivity:
public class mainactivity extends activity {
private dynamictagflowlayout dynamictagflowlayout;
list<string> tags = new arraylist<string>();
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_dynamic_tagflowlayout);
dynamictagflowlayout = (dynamictagflowlayout) findviewbyid(r.id.dynamic_tag);
dynamictagflowlayout.setontagitemclicklistener(new ontagitemclicklistener() {
@override
public void onclick(view v) {
textview tv = (textview) v;
toast.maketext(mainactivity.this, tv.gettext().tostring(), toast.length_short).show();
}
});
initdata();
dynamictagflowlayout.settags(tags);
}
private void initdata() {
tags.add("阳哥你好!");
tags.add("android开发");
tags.add("新闻热点");
tags.add("热水进宿舍啦!");
tags.add("i love you");
tags.add("成都妹子");
tags.add("新余妹子");
tags.add("仙女湖");
tags.add("创新工厂");
tags.add("孵化园");
tags.add("神州100发射");
tags.add("有毒疫苗");
tags.add("顶你阳哥阳哥");
tags.add("hello world");
tags.add("闲逛的蚂蚁");
tags.add("闲逛的蚂蚁");
tags.add("闲逛的蚂蚁");
tags.add("闲逛的蚂蚁");
tags.add("闲逛的蚂蚁");
tags.add("闲逛的蚂蚁");
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
更多android自定义viewgroup实现标签浮动效果。