Android流式布局控件

时间:2021-09-17 08:18:33

1,自定义flowlayout代码

package com.hyang.administrator.studentproject.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup; import java.util.ArrayList;
import java.util.List; /**
* Created by Administrator on 2017/6/20.
*/
public class FlowGroupView extends ViewGroup {
/**
* 储存所有的view 按行记录
*/
private List<List<View>> mAllViews = new ArrayList<List<View>>();
/**
* 记录每一行的高度
*/
private List<Integer> mLineHeight = new ArrayList<Integer>();
private String TAG = "TAG"; public FlowGroupView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
} public FlowGroupView(Context context, AttributeSet attrs) {
super(context, attrs);
} public FlowGroupView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 置空 view 容器 和 lineHeight 容器 重新赋值
//因为OnMeasure方法会走两次,第一次是实例化这个对象的时候高度和宽度都是0
//之后走了OnSizeChange()方法后 又走了一次OnMeasure,所以要把第一次加进去的数据清空。
mAllViews.clear();
mLineHeight.clear();
//得到上级容器为其推荐的宽高和计算模式
int specWidthMode = MeasureSpec.getMode(widthMeasureSpec);
int specHeighMode = MeasureSpec.getMode(heightMeasureSpec);
int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);
int specHeighSize = MeasureSpec.getSize(heightMeasureSpec);
// 计算出所有的 child 的 宽和高
// measureChildren(specWidthSize, specHeighSize);
// 记录如果是 warp_content 是设置的宽和高
int width = 0;
int height = 0;
// 得到子view的个数
int cCount = getChildCount();
/**
* 记录每一行的宽度,width不断取最大宽度
*/
int lineWidth = 0;
/**
* 每一行的高度,累加至height
*/
int lineHeight = 0; // 存储每一行所有的childView
List<View> lineViews = new ArrayList<View>(); for (int i = 0; i < cCount; i++) {
// 得到每个子View
View child = getChildAt(i);
// 测量每个子View的宽高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 当前子view的lp
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
// 子view的宽和高
int cWidth = 0;
int cheight = 0;
// 当前子 view 实际占的宽
cWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
// 当前子View 实际占的高
cheight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
lineHeight=cheight;
// 需要换行
if(lineWidth + cWidth > specWidthSize){
width = Math.max(lineWidth, cWidth);// 取最大值
lineWidth = cWidth; // 开启新行的时候重新累加width
// 开启新行时累加 height
// lineHeight = cheight; // 记录下一行的高度
mAllViews.add(lineViews);
mLineHeight.add(cheight);
lineViews = new ArrayList<>();
// 换行的时候把该 view 放进 集合里
lineViews.add(child);// 这个 view(child) 是下一行的第一个view
height += cheight; //每个View高度是一样的,直接累加
Log.e("需要换行", "hight--" + height);
Log.e("onMeasure", "AllViews.size() -- > " + mAllViews.size());
}else {
// 不需要换行
lineWidth += cWidth;//
Log.e("不需要换行","hight--"+height);
// 不需要换行时 把子View add 进集合
lineViews.add(child);
} if(i == cCount-1){
// 如果是最后一个view
width = Math.max(lineWidth, cWidth);
height += cheight;
Log.e("最后一个view","hight--"+height);
}
}
// 循环结束后 把最后一行内容add进集合中
mLineHeight.add(lineHeight); // 记录最后一行
mAllViews.add(lineViews);
// MeasureSpec.EXACTLY 表示设置了精确的值
// 如果 mode 是 MeasureSpec.EXACTLY 时候,则不是 warp_content 用计算来的值,否则则用上级布局分给它的值
setMeasuredDimension(
specWidthMode == MeasureSpec.EXACTLY ? specWidthSize : width,
specHeighMode == MeasureSpec.EXACTLY ? specHeighSize : height
);
Log.e("onMeasure", "mAllViews.size() -- > " + mAllViews.size() + " mLineHeight.size() -- > " + mLineHeight.size() + "Height -- > "+height);
} /**
* 所有childView的位置的布局
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 当前行的最大高度
int lineHeight = 0;
// 存储每一行所有的childView
List<View> lineViews = new ArrayList<View>();
int left = 0;
int top = 0;
// 得到总行数
int lineNums = mAllViews.size();
for (int i = 0; i < lineNums; i++)
{
// 每一行的所有的views
lineViews = mAllViews.get(i);
// 当前行的最大高度
lineHeight = mLineHeight.get(i); Log.e("onLayout" , "第" + i + "行 :" + lineViews.size()+"-------lineHeight"+ lineHeight); // 遍历当前行所有的View
for (int j = 0; j < lineViews.size(); j++)
{
View child = lineViews.get(j);
if (child.getVisibility() == View.GONE)
{
continue;
}
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); //计算childView的left,top,right,bottom
int lc = left + lp.leftMargin;
int tc = top + lp.topMargin;
int rc =lc + child.getMeasuredWidth();
int bc = tc + child.getMeasuredHeight(); child.layout(lc, tc, rc, bc); left += child.getMeasuredWidth() + lp.rightMargin + lp.leftMargin;
}
left = 0;
top += lineHeight;
}
Log.v("onLayout", "onLayout mAllViews.size() -- > " + mAllViews.size() + " mLineHeight.size() -- > "+ mLineHeight.size());
} /**
* 这个一定要设置,否则会包强转错误
* 设置它支持 marginLayoutParams
*/
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(),attrs);
}
}

2.在布局文件中使用

 <com.hyang.administrator.studentproject.widget.FlowGroupView
android:id="@+id/flow_view_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.hyang.administrator.studentproject.widget.FlowGroupView>

3.TextView的样式文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#E7E7E7" >
</solid>
<corners
android:radius="30dp"
/> <padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>

4.在Activity中使用

 package com.hyang.administrator.studentproject.activity;

 import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; import com.hyang.administrator.studentproject.R;
import com.hyang.administrator.studentproject.widget.FlowGroupView; import org.xutils.view.annotation.ViewInject;
import org.xutils.x; import java.util.ArrayList; public class FlowLayoutActivity extends AppCompatActivity { @ViewInject(R.id.flow_button)
private Button addTextButton;
@ViewInject(R.id.flow_view_group)
private FlowGroupView flowView; private ArrayList<String> names; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flow_layout);
x.view().inject(this); setTwoFlowLayout(); addTextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addTextView("添加1");
}
});
} private void setTwoFlowLayout() {
//添加数据
names = new ArrayList<String>();
names.add("降龙十八掌");
names.add("黯然销魂掌");
names.add("左右互搏术");
names.add("七十二路空明拳");
names.add("小无相功");
names.add("拈花指");
names.add("打狗棍法");
names.add("蛤蟆功");
names.add("九阴白骨爪");
names.add("一招半式闯江湖");
names.add("醉拳");
names.add("龙蛇虎豹");
names.add("葵花宝典");
names.add("吸星大法");
names.add("如来神掌警示牌");
//为布局添加内容
for (int i = 0; i < names.size(); i++) {
addTextView(names.get(i));
}
} /**
* 动态添加布局
* @param str
*/
private void addTextView(String str) {
TextView child = new TextView(this);
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
child.setLayoutParams(params);
child.setBackgroundResource(R.drawable.flag);
child.setText(str);
child.setTextColor(Color.WHITE);
initEvents(child);//监听
flowView.addView(child);
} /**
* 为每个view 添加点击事件
*/
private void initEvents(final TextView tv){
tv.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(FlowLayoutActivity.this, tv.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}