简单易懂的瀑布流

最原始简单易懂的瀑布流的布局方式

<?xml version="1.0" encoding="utf-8"?>


<com.example.app_work_three.TitleCustomView
android:id="@+id/title"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>

<com.example.app_work_three.TextViewCustomView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="搜索历史"
    />
<com.example.app_work_three.OnSizeCustomView
    android:id="@+id/fl_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"/>
<com.example.app_work_three.TextViewCustomView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="热门搜索"
    />
<com.example.app_work_three.OnSizeCustomView
    android:id="@+id/fl_hot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"/>
## 自定义View搜索框 包括一些获取输出狂的值 进行往Activity传值 用到的接口回调 package com.example.app_work_three;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.app_work.R;

public class TitleCustomView extends LinearLayout {

private Context context;

public TitleCustomView(Context context) {
    super(context);
    this.context = context;
    initData();
}

public TitleCustomView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    initData();
}

private void initData() {
    //首先要拿到首页面的自定义View输入框和图片
    View view = View.inflate(context, R.layout.three_title,null);
    final EditText editText = view.findViewById(R.id.edit_text);
    view.findViewById(R.id.title_serach).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mButtonLenstener!=null)
            {
                mButtonLenstener.onButtonClick(editText.getText().toString());
            }
        }
    });
    addView(view);
}

//第三不 设置成员变量

ButtonLenstener mButtonLenstener;

//第四步  并且给成员赋值
public void setOnBuutonClickListener(ButtonLenstener buttonLenstener){
    mButtonLenstener = buttonLenstener;
}
//第一步
public interface ButtonLenstener{
    //第二部步 写一个方法和参数
    void onButtonClick(String str);
}

}

设置屏幕的宽高大小 来判断我们历史记录的长度

package com.example.app_work_three;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class OnSizeCustomView extends LinearLayout {

//元素中最高的一个
private int mChildMaxHeight;

//左间距 和右间距
private int mHspace = 20;
private int mVspace = 20;


public OnSizeCustomView(Context context) {
    super(context);
}

@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);
    //便利循环每个资源的找到最高的一个
    findMaxSzieHeight();
    //初始化值
    int left = 0,top = 0;

    //循环所有的子布局
    int childCount = getChildCount();
    for(int i = 0;i<childCount;i++)
    {
        View childAt = getChildAt(i);
        //是否为一行的开头
        if(left!=0)
        {
            //需要换行 因为放不下
            if((left+childAt.getMeasuredWidth())>sizeWidth)
            {
                //计算出下一行的top
                top+=mChildMaxHeight+mVspace;
                left = 0;
            }
        }
        left+=childAt.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);

    //开始安排子元素的位置

    int top = 0,left = 0;
    int childCount = getChildCount();
    for(int i=0;i<childCount;i++)
    {
        View childAt = getChildAt(i);
        if(left!=0)
        {
            //需要换行
            if((left+childAt.getMeasuredWidth())>getWidth())
            {
                top+=mChildMaxHeight+mVspace;
                left = 0;
            }
        }
        //安排孩子的位置
        childAt.layout(left,top,left+childAt.getMeasuredWidth(),top+mChildMaxHeight);
        left+=childAt.getMeasuredWidth()+mHspace;
    }
}

private void findMaxSzieHeight() {
    mChildMaxHeight = 0;
    int childCount = getChildCount();
    for(int i = 0;i<childCount;i++)
    {
        View childAt = getChildAt(i);
        if(childAt.getMeasuredHeight()>mChildMaxHeight)
        {
            mChildMaxHeight=childAt.getMeasuredHeight();
        }
    }
}

public OnSizeCustomView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

}

布局使用xml来调理不同的样式

package com.example.app_work_three;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

import com.example.app_work.R;

@SuppressLint(“AppCompatCustomView”)
public class TextViewCustomView extends TextView {

public TextViewCustomView(Context context) {
    super(context);
}

public TextViewCustomView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);


    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WeekFlowLayout);
    int color = typedArray.getColor(R.styleable.WeekFlowLayout_textColor, Color.BLACK);

    setTextColor(color);

    typedArray.recycle();
}

}

Activity操作package com.example.app_work_three;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.app_work.R;

import java.util.UUID;

public class MainAtivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.three_activity);

    final OnSizeCustomView fl_search = findViewById(R.id.fl_search);
    OnSizeCustomView fl_hot = findViewById(R.id.fl_hot);

    TitleCustomView title = findViewById(R.id.title);

    title.setOnBuutonClickListener(new TitleCustomView.ButtonLenstener() {
        @Override
        public void onButtonClick(String str) {
            final UUID uuid = UUID.randomUUID();
            TextView textView = new TextView(MainAtivity.this);
            textView.setTag(uuid);
            textView.setTextColor(Color.RED);
            textView.setText(str);
            textView.setBackgroundResource(R.drawable.edit_bg);
            fl_search.addView(textView);

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uuid = String.valueOf(v.getTag());
                    Toast.makeText(MainAtivity.this,uuid,Toast.LENGTH_LONG).show();
                }
            });
        }
    });

    for(int i = 0;i<30;i++)
    {
        TextView textView = new TextView(MainAtivity.this);
        textView.setText("数据"+i);
        textView.setTextColor(Color.RED);
        textView.setBackgroundResource(R.drawable.edit_bg);
        fl_hot.addView(textView);
    }
}

}

猜你喜欢

转载自blog.csdn.net/qq_42960779/article/details/84727482