流布局,相信大家都知道,就是依次展示,每行展示的个数不定,此行不能展示出末尾的那个item,自动换行,我简单写了一个,下面是我的效果图,大家看一下,是否是自己要找的效果
下面贴出我的自定义flowLayout布局
/**
* 打造一款具有滑动,并且不同高度的view会居中显示的流式布局
*/
public class FlowLayout extends ViewGroup implements FlowNotification {
private static final String TAG = "yaya";
//主要用来处理滑动用的,正直才可以滑动的
protected int verticalOffset;
protected int width, height;
private LineDes row = new LineDes();
protected int totalHeight;
//用来存储每一行的高度,主要让子类去实现几行的流式布局
protected SparseArray<Float> heightLines = new SparseArray<>();
private FlowAdapter flowAdapter;
private boolean isLineCenter;
private static final boolean default_line_center = false;
@Override
public void onChange() {
if (flowAdapter != null) {
addItemView(flowAdapter);
}
}
//用来存储信息的
private class LineDes {
//当前行的顶点坐标
float cuLineTop;
//行高度以最高的view为基准
int lineHeight;
//一行收集到的item
List<ItemView> views = new ArrayList<>();
//用完了该行的信息后进行清除的操作
public void clearLineDes() {
if (views.size() > 0) {
views.clear();
}
lineHeight = 0;
cuLineTop = 0;
}
}
private class ItemView {
View view;
int heightArea;
public ItemView(View view, int heightArea) {
this.view = view;
this.heightArea = heightArea;
}
}
public FlowLayout(Context context) {
this(context, null);
}
public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout);
isLineCenter = array.getBoolean(R.styleable.FlowLayout_is_line_center, default_line_center);
array.recycle();
initArgus(context, attrs);
}
protected void initArgus(Context context, AttributeSet attrs) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
width = measureWidth;
} else {
//以实际屏宽为标准
width = getContext().getResources().getDisplayMetrics().widthPixels;
}
Log.d(TAG, "屏高:" + getContext().getResources().getDisplayMetrics().heightPixels);
int left = getPaddingLeft();
int right = getPaddingRight()<