自定义view--流式布局

原创 2018年04月17日 14:31:35

package example.com.niuyuejiao;

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

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

/**
 * author:Created by niuyuejiao on 2018/4/16.
 */

public class MyView extends ViewGroup {

    //每行的高度
    private List<Integer> mLineHeight = new ArrayList<>();
    //储存所有的子view
    private List<List<View>> mAllChildViews = new ArrayList<>();

    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        //测量宽度
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        //测量高度
        int sizeheight = MeasureSpec.getSize(heightMeasureSpec);
        int modeheight = MeasureSpec.getMode(heightMeasureSpec);
        //如果宽高为wrap_content的情况
        int width = 0;
        int height = 0;
        //记录每一行的宽高
        int lineWidth = 0;
        int lineHeight = 0;
        //获取一个view的个数
        int childCount = getChildCount();
        //循环获得每个view的数值
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            //测量子View的宽高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            //得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            //View 占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            //View占据的高
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            //换行的时候
            if (lineWidth + childWidth > sizeWidth) {
                //对比得到最大的宽度
                width = Math.max(width, lineWidth);
                //重置lineWidth
                lineWidth = childWidth;
                //记录行高
                height += lineHeight;
                lineHeight = childHeight;
            } else {
                //叠加行宽
                lineWidth += childWidth;
                //得到最大行高
                lineHeight = Math.max(lineHeight, childHeight);
            }
            //处理最后一个子View
            if (i == childCount - 1) {
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }
        //wrap_content
        setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width, modeheight == MeasureSpec.EXACTLY ? sizeheight : height);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    //设置布局
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        mAllChildViews.clear();
        mLineHeight.clear();

        //获得当前ViewGroup的宽度
        int width = getWidth();

        int lineWidth = 0;
        int lineHeight = 0;
        // 记录当前行的View
        List<View> lineViews = new ArrayList<View>();
        int childCount = getChildCount();
        for (int f = 0; f < childCount; f++) {
            View child = getChildAt(f);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            //如果需要换行
            if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width) {
                //记录LineHeight
                mLineHeight.add(lineHeight);
                //记录当前的Views
                mAllChildViews.add(lineViews);
                //重置行高
                lineWidth = 0;
                lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
                //重置view的集合
                lineViews = new ArrayList();
            }
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);
            lineViews.add(child);
        }
        //处理最后一行
        mLineHeight.add(lineHeight);
        mAllChildViews.add(lineViews);

        //设置子View的位置
        int left = 20;
        int top = 100;
        //获取行数
        int lineCount = mAllChildViews.size();
        for (int d = 0; d < lineCount; d++) {
            //当前行的Views和高度
            lineViews = mAllChildViews.get(d);
            lineHeight = mLineHeight.get(d);

            for (int j = 0; j < lineViews.size(); j++) {
                View child = lineViews.get(j);
                //判断是否显示
                if (child.getVisibility() == View.GONE) {
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int cLeft = left + lp.leftMargin;
                int cTop = top + lp.topMargin;
                int cRight = cLeft + child.getMeasuredWidth();
                int cBottom = cTop + child.getMeasuredHeight();
                //进行子View进行布局
                child.layout(cLeft, cTop, cRight, cBottom);
                left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            }
            left = 20;
            top += lineHeight;
        }
    }

    /**
     * 与当前ViewGroup对应的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub

        return new MarginLayoutParams(getContext(), attrs);
    }

}
ackage example.com.niuyuejiao;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends BaseActivity {

    private String[] datas = {"笔记本", "空气净化器", "安卓手机", "超级大号圆珠笔", "空气滤芯", "超级大号钢笔"};
    private Button clear;
    private MyView main_view;
    private EditText editText;
    private ImageView add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        main_view = findViewById(R.id.main_view);
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = 5;
        lp.rightMargin = 5;
        lp.topMargin = 5;
        lp.bottomMargin = 5;
        for (int i = 0; i < datas.length; i++) {
            TextView textView = new TextView(MainActivity.this);
            textView.setText(datas[i]);
            textView.setTextColor(Color.BLUE);
            textView.setTextSize(18);
            textView.setBackgroundResource(R.drawable.seleter);
            main_view.addView(textView, lp);
        }
    }
<?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="match_parent"
    android:orientation="vertical"
    tools:context="example.com.niuyuejiao.MainActivity">



        <example.com.niuyuejiao.MyView
            android:id="@+id/main_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </example.com.niuyuejiao.MyView>
  
</LinearLayout>

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niu_yue_jiao/article/details/79974451

Android自定义ViewGroup实现流式布局

实现宽度不足自动换行的流式布局: FlowLayout.java package com.jackie.flowlayout; import android.content.Context; imp...
  • shineflowers
  • shineflowers
  • 2015-08-28 18:02:44
  • 2117

android 自定义view实现流式布局

android 自定义view实现流式布局
  • coderinchina
  • coderinchina
  • 2015-12-15 16:10:15
  • 900

【Android】掌握自定义LayoutManager(二) 实现流式布局

转载请标明出处: http://blog.csdn.net/zxt0601/article/details/52956504 本文出自:【张旭童的博客】 本系列文章相关代码传送门: 自...
  • zxt0601
  • zxt0601
  • 2016-10-28 17:58:17
  • 16178

Android自定义控件之流式布局

脑筋急转: 在一个房间里,有油灯 ,暖炉及壁炉。现在,想要用一根火柴将三个器具点燃,请问首先应该点燃哪一个? 请查看文章最后有有解析...
  • zl18603543572
  • zl18603543572
  • 2016-03-12 22:28:13
  • 2176

Android等级进度条demo

  • 2016年12月30日 20:00
  • 220KB
  • 下载

Android从零开搞系列:自定义View(10)流式布局

流式布局,拆解一个优秀的GitHub框架。
  • wjzj000
  • wjzj000
  • 2017-03-25 12:49:42
  • 543

Android自定义控件--流式布局(FlowLayout)--自动适配

在android开发中,随着开发需求的不断提升,android原生的控件在很大程度上已不能满足开发者以及用户的需求,为了更好的增加用户体验,更有利的维护UI,在一个完整的程序中,自定义控件往往是不可或...
  • MyLoveyaqiong
  • MyLoveyaqiong
  • 2016-11-03 01:37:59
  • 3390

Android中的自定义View(二)之 流式布局实现

我们在上一篇文章《Android中的自定义View》中介绍过自定义View的几种方式,并通过示例一演示了“直接继承View”的自定义View的使用情况。今天接着来介绍自定义View里稍为复杂的“直接继...
  • lyz_zyx
  • lyz_zyx
  • 2017-10-10 11:52:09
  • 254

Android学习笔记:自定义实现流式布局

前几天在开发项目的时候,有一个需求是展示历史搜索启示 ,展示的样式是像瀑布流一样(每一行展示的控件个数根据控件的内容不同而不相同,当一行展示满后,自动换行展示)。最开始是自定义LinearLayout...
  • true100
  • true100
  • 2015-12-29 11:09:31
  • 2016

FlowLayout流式布局实现搜索清空历史记录

效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中,每次进入页面都先判断sp里是否有值并展示首先需要导入一个module,下载地址:https://...
  • thw_m
  • thw_m
  • 2018-02-22 15:00:07
  • 124
收藏助手
不良信息举报
您举报文章:自定义view--流式布局
举报原因:
原因补充:

(最多只允许输入30个字)