1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
| import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup;
import java.util.List;
public class ZTagView extends ViewGroup { private ZTagItem.OnTagClickListener mTagClickListener; private int mVerticalInterval; private int mHorizontalInterval;
private int columnNum = 3;
private int mTextSize = 14; private int mTextColor = Color.parseColor("#ff000000"); private int mTextSelectColor = Color.parseColor("#ff219EFF"); private int mBgColor = Color.parseColor("#ffF7F7F7"); private int mBgSelectColor = Color.parseColor("#ffEBF8FF"); private int mBorderColor = Color.parseColor("#ffF7F7F7"); private int mBorderSelectColor = Color.parseColor("#ffEBF8FF");
public ZTagView(Context context) { this(context, null); }
public ZTagView(Context context, AttributeSet attrs) { this(context, attrs, -1); }
public ZTagView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); _init(context); }
public void init(int textSize, int textColor, int textSelectColor, int bgColor, int bgSelectColor, int borderColor, int borderSelectColor) { mTextSize = textSize; mTextColor = textColor; mTextSelectColor = textSelectColor; mBgColor = bgColor; mBgSelectColor = bgSelectColor; mBorderColor = borderColor; mBorderSelectColor = borderSelectColor; }
public void setTagClickListener(ZTagItem.OnTagClickListener tagClickListener) { mTagClickListener = tagClickListener; }
private int dp2px(Context context, float dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics()); }
private void _init(Context context) { mHorizontalInterval = dp2px(context, 10); L.e("mHorizontalInterval:" + mHorizontalInterval); mVerticalInterval = dp2px(context, 6); setWillNotDraw(false); setPadding(mHorizontalInterval, mVerticalInterval, mHorizontalInterval, mVerticalInterval); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int availableWidth = widthSpecSize - getPaddingLeft() - getPaddingRight(); measureChildren(widthMeasureSpec, heightMeasureSpec); int childCount = getChildCount(); int tmpWidth = 0; int measureHeight = 0; int maxLineHeight = 0; for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (maxLineHeight == 0) { maxLineHeight = child.getMeasuredHeight(); } else { maxLineHeight = Math.max(maxLineHeight, child.getMeasuredHeight()); }
int width = child.getMeasuredWidth(); if (columnNum > 0) { width = (int) ((double) (availableWidth - mHorizontalInterval * (columnNum - 1)) / columnNum); } tmpWidth += width + mHorizontalInterval; if (tmpWidth - mHorizontalInterval > availableWidth) { measureHeight += maxLineHeight + mVerticalInterval; maxLineHeight = child.getMeasuredHeight(); tmpWidth = 0; }
} measureHeight += maxLineHeight; measureHeight += getPaddingTop() + getPaddingBottom(); if (childCount == 0) { setMeasuredDimension(0, 0); } else if (heightSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(widthSpecSize, measureHeight); } else { setMeasuredDimension(widthSpecSize, heightSpecSize); } }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); if (childCount <= 0) { return; } int totalWidth = getMeasuredWidth(); int availableWidth = totalWidth - getPaddingLeft() - getPaddingRight(); int curTop = getPaddingTop(); int curLeft = getPaddingLeft(); int maxHeight = 0; for (int i = 0; i < childCount; i++) { View itemChildView = getChildAt(i);
if (maxHeight == 0) { maxHeight = itemChildView.getMeasuredHeight(); } else { maxHeight = Math.max(maxHeight, itemChildView.getMeasuredHeight()); }
int width = itemChildView.getMeasuredWidth(); if (columnNum > 0) { width = (int) ((double) (availableWidth - mHorizontalInterval * (columnNum - 1)) / columnNum); } int height = itemChildView.getMeasuredHeight(); if (curLeft + width > totalWidth - getPaddingRight()) { curLeft = getPaddingLeft(); curTop += maxHeight + mVerticalInterval; maxHeight = itemChildView.getMeasuredHeight(); } itemChildView.layout(curLeft, curTop, curLeft + width, curTop + height); curLeft += width + mHorizontalInterval; } }
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); }
public void cleanTags() { removeAllViews(); postInvalidate(); }
public void addTag(ZTagVo tag) { int tagIndex = getChildCount(); ZTagItem tagItem = null; if (tag.isSeleced()) { tagItem = new ZTagItem( getContext(), tagIndex, tag, mTextSize, mTextSelectColor, mBgSelectColor, mBorderSelectColor );
} else { tagItem = new ZTagItem( getContext(), tagIndex, tag, mTextSize, mTextColor, mBgColor, mBorderColor ); } tagItem.setTagClickListener(new ZTagItem.OnTagClickListener() { @Override public void onTagClick(int index, ZTagVo tagVo) { if (mTagClickListener != null) { mTagClickListener.onTagClick(index, tagVo); } }
@Override public void onTagLongClick(int index, ZTagVo tagVo) { if (mTagClickListener != null) { mTagClickListener.onTagLongClick(index, tagVo); } } });
addView(tagItem);
}
public void addTags(List<ZTagVo> tags) { for (ZTagVo tag : tags) { addTag(tag); } }
public void setTags(List<ZTagVo> tags) { cleanTags(); addTags(tags); }
public int getAvailableWidth() { return getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); } }
|