AutoGridLayout(自动滑动GridLayout)

package autochangelineview.app.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
* Created by gong sheng on 2016/12/27
*/
public class FixFlowLayout extends ViewGroup {
public FixFlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

int mColumnCount = 4;

public List<View> getShowView() {
List<View> list = new ArrayList<View>();
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
if (getChildAt(i).getVisibility() == VISIBLE) {
list.add(getChildAt(i));
}
}
return list;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
List<View> showView = getShowView();
int childCount = showView.size();
//光标
int top = getPaddingTop();
int left = getPaddingLeft();
for (int i = 0; i < childCount; i++) {
View view = showView.get(i);
//排列
view.layout(left, top, left + view.getMeasuredWidth(), top + view.getMeasuredHeight());
if (i == 0) {
left += view.getMeasuredWidth();
} else if (i % mColumnCount == 0) {
left += view.getMeasuredWidth();
} else if (mColumnCount - i % mColumnCount == 1) {
left = getPaddingLeft();
top += view.getMeasuredHeight();
} else {
left += view.getMeasuredWidth();
}
}
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int childCount = getChildCount();
//高度测量模式
int childHeightMeasureSpec = 0;
//宽度测量模式
int childWidthMeasureSpec = 0;
//测量子view并重新赋值
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
childHeightMeasureSpec =
MeasureSpec.makeMeasureSpec(childView.getMeasuredHeight(), MeasureSpec.AT_MOST);
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childView.getMeasuredWidth(), MeasureSpec.EXACTLY);
childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}

@Override
public void addView(View child, LayoutParams params) {
super.addView(child, params);
}
}

转载于:https://www.cnblogs.com/g-sheng/p/6227932.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值