每项多少个固定对其 流式布局 改编

1.    正常的流式布局

package widgets;

import java.util.ArrayList;
import java.util.HashMap;


import com.sensu.automall.R;
import com.sensu.automall.mode.FlowModel;
import com.sensu.automall.utils.MassageUtils;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
 * 流式布局
 * 
 * @author CHENSHI
 * 
 */
public class FlowLayout_L extends ViewGroup {
private int mHorizontalSpacing;
private int mVerticalSpacing;
int line_height = 0;
private Context context;
public static HashMap map=new HashMap();


public FlowLayout_L(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(width, MeasureSpec.AT_MOST),
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 setDate(ArrayList list) {
for (int i = 0; i < list.size(); i++) {
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView txt=new TextView(context);
if(list.get(i).getState().equals("1")){
Drawable drawable = getResources().getDrawable(R.drawable.duihao);
drawable.setBounds(0, 0, MassageUtils.dip2px(14), MassageUtils.dip2px(14)); 
txt.setCompoundDrawables(drawable, null, null, null);
}else{
Drawable drawable = getResources().getDrawable(R.drawable.cross_gray);
drawable.setBounds(0, 0, MassageUtils.dip2px(14), MassageUtils.dip2px(14)); 
txt.setCompoundDrawables(drawable, null, null, null);
}
txt.setCompoundDrawablePadding(MassageUtils.dip2px(5));
txt.setText(list.get(i).getStr());
txt.setGravity(Gravity.CENTER);
txt.setTextColor(Color.BLACK);
txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
txt.setClickable(true);
txt.setEnabled(true);
this.addView(txt, params);
}
invalidate();
}



public interface ItemtxtOnclick{
void txtOnClick(TextView self_potion,int group_pos,int postion);
}
ItemtxtOnclick itemtxtOnclick;
public void setItemtxtOnclick(ItemtxtOnclick itemtxtOnclick){
this.itemtxtOnclick=itemtxtOnclick;
}


}




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 map = new 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 cataLogs,final boolean header){
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 brands,boolean header){


}




  public void setDate(final List list, boolean header) {
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;
}


}


你可能感兴趣的:(Android)