MainActivity
package com.example.fallslist_demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
final FlowLayoutText flow_one = findViewById(R.id.flow_one);
FlowLayoutText flow_two = findViewById(R.id.flow_two);
final TitleChildView title_child = findViewById(R.id.title_child);
title_child.setButtonListener(new TitleChildView.OnButtonListener() {
@Override
public void OnButtonClick(String str) {
TextView textView = new TextView(MainActivity.this);
textView.setText(str);
textView.setBackgroundResource(R.drawable.edit_bg);
flow_one.addView(textView);
}
});
for (int i = 0; i < 30 ; i++) {
TextView textView = new TextView(MainActivity.this);
textView.setText("频道瀑布流"+i);
textView.setBackgroundResource(R.drawable.edit_bg);
flow_two.addView(textView);
}
}
}
TitleView
package com.example.fallslist_demo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class TitleView extends LinearLayout {
Context mContext;
public TitleView(Context context) {
super(context);
mContext = context;
}
public TitleView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
//标题:历史、推荐
private void init() {
View view = View.inflate(mContext, R.layout.title_name, null);
}
}
TitleChildView实现
package com.example.fallslist_demo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import static com.example.fallslist_demo.R.id.edit_title;
public class TitleChildView extends LinearLayout {
Context mContext;
public TitleChildView(Context context) {
super(context);
mContext = context;
init();
}
public TitleChildView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
//设置 标题
private void init() {
View view = View.inflate(mContext, R.layout.title_child_name, null);
final EditText editText = view.findViewById(R.id.edit_title);
view.findViewById(R.id.search_title).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//为接口里的方法赋值
if (mOnButtonListener != null){
mOnButtonListener.OnButtonClick(editText.getText().toString());
}
}
});
addView(view);
}
//成员变量
OnButtonListener mOnButtonListener;
public void setButtonListener(OnButtonListener onButtonListener){
mOnButtonListener = onButtonListener;
}
//定义接口
public interface OnButtonListener{
void OnButtonClick(String str);
}
}
TextViewName内容
package com.example.fallslist_demo;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;
@SuppressLint("AppCompatCustomView")
public class TextViewName extends TextView {
public TextViewName(Context context) {
super(context);
}
public TextViewName(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FlowLayoutText);
int color = typedArray.getColor(R.styleable.FlowLayoutText_textColor, Color.BLACK);
setTextColor(color);
typedArray.recycle();
}
}
FlowLayoutText排布
package com.example.fallslist_demo;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class FlowLayoutText extends LinearLayout {
/**
* 孩子中最高的一个
*/
private int mChildMaxHeight;
/**
* 每一个孩子的左右的间距
* 20是默认值,单位是px
*/
private int mHSpace = 20;
/**
* 每一行的上下的间距
* 20是默认值,单位是px
*/
private int mVSpace = 20;
public FlowLayoutText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取父容器推荐屏幕的宽高,计算模式
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
//测量孩子的宽高
measureChildren(widthMeasureSpec, heightMeasureSpec);
//定义方法,存入最高的孩子
findMaxChildHeight();
//初始化边距, left, top
int left = 0, top = 0;
//开始循环遍历所有孩子, 获取总数
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if (left != 0){
//一行已经放不下了,要换行
if ((left + view.getMeasuredWidth()) > sizeWidth){
//计算下一行的高边top
top += mChildMaxHeight + mVSpace;
left = 0;
}
}
left += view.getMeasuredWidth() + mHSpace;
}
setMeasuredDimension(sizeWidth, (top + mChildMaxHeight) > sizeHeight ? sizeHeight : top + mChildMaxHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findMaxChildHeight();
//放置孩子们的位置
//初始化 left, top
int left = 0, top = 0;
//开始循环遍历所有孩子, 获取总数
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if (left != 0) {
//一行已经放不下了,要换行
if ((left + view.getMeasuredWidth()) > getWidth()) {
//计算下一行的高边top
top += mChildMaxHeight + mVSpace;
left = 0;
}
}
//放置孩子的位置
view.layout(left, top, left + view.getMeasuredWidth(), top + mChildMaxHeight);
left += view.getMeasuredWidth() + mHSpace;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
/**
* 找到所有孩子的最高
*/
public void findMaxChildHeight(){
mChildMaxHeight = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if (view.getMeasuredHeight() > mChildMaxHeight){
//重新赋值
mChildMaxHeight = view.getMeasuredHeight();
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffc"
tools:context=".MainActivity">
<com.example.fallslist_demo.TitleChildView
android:id="@+id/title_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<com.example.fallslist_demo.TextViewName
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="搜索历史"
android:textSize="20sp"
/>
<com.example.fallslist_demo.FlowLayoutText
android:id="@+id/flow_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
/>
<com.example.fallslist_demo.TextViewName
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="热门搜索"
android:layout_marginTop="10dp"
android:textSize="20sp"
/>
<com.example.fallslist_demo.FlowLayoutText
android:id="@+id/flow_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
/>
</LinearLayout>
title_name.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
title_child_name.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/search_title"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:background="@drawable/search"
/>
<EditText
android:id="@+id/edit_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/search_title"
/>
</RelativeLayout>
attrs.xml自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--name 为想调用此属性的类名 -->
<declare-styleable name="FlowLayoutText">
<attr name="textColor" format="color"/>
</declare-styleable>
</resources>