android(粗糙)流式布局

package com.example.msi.myviewgroup;

import android.annotation.TargetApi;

import android.content.Context;import android.graphics.Color;import android.os.Build;

import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;

import android.view.View;import android.view.ViewGroup;import android.widget.TextView;

import java.util.List;

/** * Created by MSI on 2016/11/21. * 创建流式布局 */

public class FlowViewGroup extends ViewGroup {   

int width;   

OnTouch onTouch; 

  public FlowViewGroup(Context context) {     

  super(context);   

}   

public FlowViewGroup(Context context, AttributeSet attrs) {   

    super(context, attrs);   

}   

public FlowViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { 

      super(context, attrs, defStyleAttr); 

  }  /**

* 绘制展示的位置

*

* @param b

* @param i

* @param i1

* @param i2

* @param i3

*/

@Override

protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

int linewidth = 0;

int lineheight = 0;

int height = 0;

int location = 0;

if (getChildCount() > 0) {

lineheight = getChildAt(0).getMeasuredHeight() + getChildAt(0).getPaddingTop() + getChildAt(0).getPaddingBottom();

height = getChildAt(0).getMeasuredHeight() + getChildAt(0).getPaddingTop() + getChildAt(0).getPaddingBottom();

}

for (int z = 0; z < getChildCount(); z++) {

final View childAt = getChildAt(z);

if (linewidth + childAt.getMeasuredWidth() + childAt.getPaddingLeft() + childAt.getPaddingRight() <= width) {

linewidth += childAt.getMeasuredWidth() + childAt.getPaddingLeft() + childAt.getPaddingRight();

childAt.layout(location, lineheight - height, linewidth, lineheight);

location = linewidth;

} else {

location = 0;

linewidth = childAt.getMeasuredWidth() + childAt.getPaddingLeft() + childAt.getPaddingRight();

lineheight += childAt.getMeasuredHeight() + childAt.getPaddingTop() + childAt.getPaddingBottom();

height = lineheight - (childAt.getMeasuredHeight() + childAt.getPaddingTop() + childAt.getPaddingBottom());

childAt.layout(location, lineheight - height, linewidth, lineheight);

location = linewidth;

}

}

}


class MyTextView extends TextView {

public String zid;

public String getZid() {

return zid;

}

public void setZid(String zid) {

this.zid = zid;

}

public MyTextView(Context context) {

super(context);

}

public MyTextView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

public boolean dispatchTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

if (onTouch != null)

onTouch.onClick(getText() + "");

break;

}

return super.dispatchTouchEvent(event);

}

}

public void setOnClickTouchListener(OnTouch onTouch) {

this.onTouch = onTouch;

}

public interface OnTouch {

public void onClick(String str);

}

}


附上代码

你可能感兴趣的:(android(粗糙)流式布局)