欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android模仿微信收藏文件的标签处理功能

程序员文章站 2024-03-02 11:13:16
 最近需要用到微信的标签功能(如下图所示)。该功能可以添加已有标签,也可以自定义标签。也可以删除已编辑菜单。研究了一番。发现还是挺有意思的,模拟实现相关功能。...

 最近需要用到微信的标签功能(如下图所示)。该功能可以添加已有标签,也可以自定义标签。也可以删除已编辑菜单。研究了一番。发现还是挺有意思的,模拟实现相关功能。

Android模仿微信收藏文件的标签处理功能Android模仿微信收藏文件的标签处理功能

该功能使用类似flowlayout的功能。flowlayout为一个开源软件(https://github.com/apmem/android-flowlayout ),功能为自动换行的布局类型

Android模仿微信收藏文件的标签处理功能Android模仿微信收藏文件的标签处理功能

import android.content.context; 
import android.util.attributeset; 
import android.view.view; 
import android.view.viewgroup; 
/** 
* 
* @author raw 
*/ 
public class flowlayout extends viewgroup { 
private final static int pad_h = 2, pad_v = 2; // space between child views. 
private int mheight; 
public flowlayout(context context) { 
super(context); 
} 
public flowlayout(context context, attributeset attrs) { 
super(context, attrs); 
} 
@override 
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { 
assert (measurespec.getmode(widthmeasurespec) != measurespec.unspecified); 
final int width = measurespec.getsize(widthmeasurespec) - getpaddingleft() - getpaddingright(); 
int height = measurespec.getsize(heightmeasurespec) - getpaddingtop() - getpaddingbottom(); 
final int count = getchildcount(); 
int xpos = getpaddingleft(); 
int ypos = getpaddingtop(); 
int childheightmeasurespec; 
if(measurespec.getmode(heightmeasurespec) == measurespec.at_most) 
childheightmeasurespec = measurespec.makemeasurespec(height, measurespec.at_most); 
else 
childheightmeasurespec = measurespec.makemeasurespec(0, measurespec.unspecified); 
mheight = 0; 
for(int i = 0; i < count; i++) { 
final view child = getchildat(i); 
if(child.getvisibility() != gone) { 
child.measure(measurespec.makemeasurespec(width, measurespec.at_most), childheightmeasurespec); 
final int childw = child.getmeasuredwidth(); 
mheight = math.max(mheight, child.getmeasuredheight() + pad_v); 
if(xpos + childw > width) { 
xpos = getpaddingleft(); 
ypos += mheight; 
} 
xpos += childw + pad_h; 
} 
} 
if(measurespec.getmode(heightmeasurespec) == measurespec.unspecified) { 
height = ypos + mheight; 
} else if(measurespec.getmode(heightmeasurespec) == measurespec.at_most) { 
if(ypos + mheight < height) { 
height = ypos + mheight; 
} 
} 
height += 5; // fudge to avoid clipping bottom of last row. 
setmeasureddimension(width, height); 
} // end onmeasure() 
@override 
protected void onlayout(boolean changed, int l, int t, int r, int b) { 
final int width = r - l; 
int xpos = getpaddingleft(); 
int ypos = getpaddingtop(); 
for(int i = 0; i < getchildcount(); i++) { 
final view child = getchildat(i); 
if(child.getvisibility() != gone) { 
final int childw = child.getmeasuredwidth(); 
final int childh = child.getmeasuredheight(); 
if(xpos + childw > width) { 
xpos = getpaddingleft(); 
ypos += mheight; 
} 
child.layout(xpos, ypos, xpos + childw, ypos + childh); 
xpos += childw + pad_h; 
} 
} 
} // end onlayout() 
}

以上所述是小编给大家介绍的android模仿微信收藏文件的标签处理功能,希望对大家有所帮助