java布局中最简单的是FlowLayout流布局,其中组件根据设置的对齐方式从左到右排列,一行填满后排在下一行之后。
让我们直接看一下代码:
package com.per.flowlayout;
import android.content.Context;
import Android.content.RES.typed array;
import Android.database.dataset观察器;
import Android.util.attributes et;
import android.view.View;
import android.view.ViewGroup;
import Android.widget.base adapter;
import com.meetime.etherflyer.R;
//*
* @ author :东文龙
* @description:流布局-标签流容器
* @projectName: FlowLayout
* @date: 2018-12-06
* @time: 10:21
*/
publicclassflowlayoutextendsviewgroup {
//*
*各item的纵向间距
*/
private int mVerticalSpacing;
//*
*每个项目的横向间距
*/
private int mHorizontalSpacing;
private BaseAdaptermAdapter;
privatetagitemclicklistenermlistener
privatedatachangeobservermobserver;
公共流(上下文上下文) {
this (上下文,空);
}
公共流(context context,AttributeSet attrs ) )。
this(context,attrs,0 );
}
公共流(context context,AttributeSet attrs,int defStyle ) {
super(context,attrs,defStyle );
//*
*获取定义的自定义样式属性
*/
TypedArray a=context.getTheme ().obtainstyledattributes (attrs,R.styleable.FlowLayout,defStyle,0 );
for(intI=0; i a.getIndexCount (; I ) {
intattr=a.getindex(I;
sitch(attr ) {
case 0:
mhorizontalspacing=a.getdimensionpixelsize (0,10 ); //10
黑;
case 1:
mverticalspacing=a.getdimensionpixelsize (1,5 ); //5
黑;
}
}
a.recycle (;
}
//*
*设置子控件的测量模式和大小,并为所有子控件设置自己的宽度和高度
*/
@Override
protectedvoidonmeasure (intwidthmeasurespec,int heightMeasureSpec ) {
//*
*默认宽度是父容器设置的测量宽度
*/
intwidth=view.measurespec.getsize (widthmeasurespec;
//*
*贵
*/
int height=0;
//*
*每行的高度累计到height
*/
int lineHeight=0;
//*
在warp_content的情况下,记录当前childView的左侧位置
*/
int childLeft=getPaddingLeft (;
//*
在warp_content的情况下,记录当前的childVie
w的上边的一个位置*/
int childTop = getPaddingTop();
// getChildCount得到子view的数目,遍历循环出每个子View
for (int i =0; i < getChildCount(); i++) {
//拿到index上的子view
View childView = getChildAt(i);
// 测量每一个child的宽和高
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
//当前子空间实际占据的高度
int childHeight = childView.getMeasuredHeight();
//当前子空间实际占据的宽度
int childWidth = childView.getMeasuredWidth();
lineHeight = Math.max(childHeight, lineHeight);// 取最大值
//如果加入当前childView,超出最大宽度,则将目前最大宽度给width,类加height 然后开启新行
if (childWidth + childLeft + getPaddingRight() > width) {
childLeft = getPaddingLeft();// 重新开启新行,开始记录childLeft
childTop +=mVerticalSpacing + childHeight + lineHeight/2+30;// 叠加当前的高度
lineHeight = childHeight;// 开启记录下一行的高度
}else{
//否则累加当前childView的宽度
childLeft += childWidth +mHorizontalSpacing;
}
}
height += childTop + lineHeight + getPaddingBottom();
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = r - l;
int childLeft = getPaddingLeft();
int childTop = getPaddingTop();
int lineHeight =0;
//遍历所有childView根据其宽和高,计算子控件应该出现的位置
for (int i =0; i < getChildCount(); i++) {
final View childView = getChildAt(i);
if (childView.getVisibility() == View.GONE) {
continue;
}
int childWidth = childView.getMeasuredWidth();
int childHeight = childView.getMeasuredHeight();
lineHeight = Math.max(childHeight, lineHeight);
// 如果已经需要换行
if (childLeft + childWidth > width) {
childLeft = getPaddingLeft();
childTop +=mVerticalSpacing + lineHeight;
lineHeight =mVerticalSpacing + lineHeight;
}
childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
childLeft += childWidth +mVerticalSpacing;
}
}
@Override
public ViewGroup.LayoutParamsgenerateLayoutParams(AttributeSet attrs) {
return new ViewGroup.LayoutParams(this.getContext(), attrs);
}
private void drawLayout() {
if (mAdapter ==null ||mAdapter.getCount() ==0) {
return;
}
removeAllViews();
for (int i =0; i
View view =mAdapter.getView(i, null, null);
final int position = i;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener !=null) {
mListener.itemClick(position);
}
}
});
addView(view);
}
}
public void setAdapter(BaseAdapter adapter) {
if (mAdapter ==null) {
mAdapter = adapter;
if (mObserver ==null) {
mObserver =new DataChangeObserver();
mAdapter.registerDataSetObserver(mObserver);
}
drawLayout();
}
}
public void setItemClickListener(TagItemClickListener mListener) {
this.mListener = mListener;
}
public interface TagItemClickListener {
void itemClick(int position);
}
class DataChangeObserverextends DataSetObserver {
@Override
public void onChanged() {
drawLayout();
}
@Override
public void onInvalidated() {
super.onInvalidated();
}
}
}
欢迎线下交流:WX:dwl-1591293009