1. 正常的流式布局
package widgets;
import java.util.ArrayList;}
2.更改过的流逝布局,每行3个
package widgets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.sensu.automall.R;
import com.sensu.automall.mode.Brand;
import com.sensu.automall.mode.CataLog;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
/**
* 流式布局
*
* @author CHENSHI
*
*/
public class FlowLayout extends ViewGroup {
private int mHorizontalSpacing;
private int mVerticalSpacing;
int line_height = 0;
private Context context;
public static HashMap
public static String catalogID="";
public String getCatalogID(){
return catalogID;
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FlowLayout);
try {
mHorizontalSpacing = a.getDimensionPixelSize(
R.styleable.FlowLayout_horizontalSpacing, 0);
mVerticalSpacing = a.getDimensionPixelSize(
R.styleable.FlowLayout_verticalSpacing, 0);
} finally {
a.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);
int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft()
- getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop()
- getPaddingBottom();
final int count = getChildCount();
int line_height = 0;
int xpos = getPaddingLeft();
int ypos = getPaddingTop();
int childHeightMeasureSpec;
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
MeasureSpec.AT_MOST);
} else {
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
}
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
child.measure(MeasureSpec.makeMeasureSpec(
(int) ((getScreenWidth() - dip2px(100)) / 3f),
MeasureSpec.EXACTLY), childHeightMeasureSpec);
final int childw = child.getMeasuredWidth();
line_height = Math.max(line_height, child.getMeasuredHeight()
+ mVerticalSpacing);
if (xpos + childw > width) {
xpos = getPaddingLeft();
ypos += line_height;
}
xpos += childw + mHorizontalSpacing;
}
}
this.line_height = line_height;
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
height = ypos + line_height;
} else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
if (ypos + line_height < height) {
height = ypos + line_height;
}
}
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
final int width = r - l;
int xpos = getPaddingLeft();
int ypos = getPaddingTop();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final int childw = child.getMeasuredWidth();
final int childh = child.getMeasuredHeight();
if (xpos + childw > width) {
xpos = getPaddingLeft();
ypos += line_height;
}
child.layout(xpos, ypos, xpos + childw, ypos + childh);
xpos += childw + mHorizontalSpacing;
}
}
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p.width, p.height);
}
public void setCatalog(final List
int lenght = cataLogs.size();
if (!header) {
if (cataLogs.size() > 6) {
lenght = 6;
} else {
lenght = cataLogs.size();
}
}
for (int i = 0; i < lenght; i++) {
TextView txt = new TextView(context);
txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
txt.setText(cataLogs.get(i).getCatalogName());
txt.setTextColor(getResources().getColor(R.color.txt_black));
final int pos = i;
txt.setBackgroundResource(R.drawable.flowlayout_no_select);
txt.setGravity(Gravity.CENTER);
txt.setPadding(dip2px(5), dip2px(5), dip2px(5), dip2px(5));
txt.setSingleLine(true);
txt.setEllipsize(TruncateAt.END);
txt.setClickable(true);
txt.setEnabled(true);
txt.setId(i);
txt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(cataLogs.size()<=pos ){
FlowLayout.this.removeAllViews();
}
for (int j = 0; j < FlowLayout.this.getChildCount(); j++) {
FlowLayout.this.getChildAt(j).setBackgroundResource(R.drawable.flowlayout_no_select);
}
String uid=cataLogs.get(pos).getCatalogId();
if(!TextUtils.isEmpty(catalogID)){
if(!uid.equals(catalogID)){
catalogID=cataLogs.get(pos).getUID();
v.setBackgroundResource(R.drawable.flowlayout_select);
catalogID=cataLogs.get(pos).getCatalogId();
}else{
catalogID="";
}
}else{
catalogID=cataLogs.get(pos).getCatalogId();
v.setBackgroundResource(R.drawable.flowlayout_select);
}
if (itemtxtOnclick != null) {
itemtxtOnclick.TxtOnCataLog(cataLogs.get(pos).getCatalogName(),catalogID,cataLogs.get(pos),header);
}
}
});
this.addView(txt);
}
invalidate();
}
public void setBaoYang(List
}
public void setDate(final List
int lenght = list.size();
if (!header) {
if (list.size() > 6) {
lenght = 6;
} else {
lenght = list.size();
}
}
for (int i = 0; i < lenght; i++) {
TextView txt = new TextView(context);
txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
txt.setText(list.get(i).getBrandName());
txt.setTextColor(getResources().getColor(R.color.txt_black));
final int pos = i;
if (null != map.get(list.get(i).getUID())
&& map.get(list.get(i).getUID())) {
txt.setBackgroundResource(R.drawable.flowlayout_select);
} else {
txt.setBackgroundResource(R.drawable.flowlayout_no_select);
}
txt.setGravity(Gravity.CENTER);
txt.setPadding(dip2px(5), dip2px(5), dip2px(5), dip2px(5));
txt.setSingleLine(true);
txt.setEllipsize(TruncateAt.END);
txt.setClickable(true);
txt.setEnabled(true);
txt.setId(i);
txt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(list.size()<=pos){
FlowLayout.this.removeAllViews();
}
String uid=list.get(pos).getUID();
if (null != map.get(uid)
&& map.get(uid)) {
map.put(uid, false);
v.setBackgroundResource(R.drawable.flowlayout_no_select);
} else {
v.setBackgroundResource(R.drawable.flowlayout_select);
map.put(uid, true);
}
if (itemtxtOnclick != null) {
itemtxtOnclick.txtOnClick((TextView) v, pos);
}
}
});
this.addView(txt);
}
invalidate();
}
private int getScreenWidth() {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
public int dip2px(float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public interface ItemtxtOnclick {
void txtOnClick(TextView self_potion, int pos);
void TxtOnCataLog(String str,String catalogId,CataLog Selectcatalog,boolean header);
}
ItemtxtOnclick itemtxtOnclick;
public void setItemtxtOnclick(ItemtxtOnclick itemtxtOnclick) {
this.itemtxtOnclick = itemtxtOnclick;
}
}