1 package com.test.testview; 2 3 import java.util.ArrayList; 4 5 import android.content.Context; 6 import android.graphics.Color; 7 import android.text.TextUtils; 8 import android.util.AttributeSet; 9 import android.view.View; 10 import android.widget.LinearLayout; 11 import android.widget.TextView; 12 13 import com.test.testview.R; 14 15 public class HotwordsViewgroup extends LinearLayout implements 16 View.OnClickListener 17 { 18 private final static int VIEW_MARGIN_HORIZONTAL = 50; 19 private final static int VIEW_MARGIN_VERTICAL = 20; 20 21 private HotwordsOnclickListener mHotwordsOnclickListener; 22 23 public static interface HotwordsOnclickListener 24 { 25 /** 26 * 触发onclik回调,根据TextView.getText去做相关事情 27 * 28 * @param view 29 */ 30 public void hotwordOnclick(TextView view); 31 } 32 33 public HotwordsViewgroup(Context context, AttributeSet attrs) 34 { 35 super(context, attrs); 36 init(); 37 } 38 39 public HotwordsViewgroup(Context context, AttributeSet attrs, int defStyle) 40 { 41 super(context, attrs, defStyle); 42 init(); 43 } 44 45 public HotwordsViewgroup(Context context) 46 { 47 super(context); 48 init(); 49 } 50 51 /** 52 * 设置每个关键词点击事件监听 53 * 54 * @param listener 55 */ 56 public void setHotwordOnclickListener(HotwordsOnclickListener listener) { 57 mHotwordsOnclickListener = listener; 58 } 59 60 private void init() { 61 setOrientation(HORIZONTAL); 62 } 63 64 public void setData(final ArrayList<String> list) { 65 removeAllViews(); 66 if (list != null) { 67 for (int i = 0; i < list.size(); i++) { 68 String content = list.get(i); 69 if (TextUtils.isEmpty(content)) { 70 continue; 71 } 72 TextView textview = new TextView(getContext()); 73 textview.setTextColor(Color.WHITE); 74 textview.setBackgroundResource(R.drawable.hot_words_bg); 75 textview.setTextSize(18); 76 textview.setOnClickListener(this); 77 textview.setMinimumWidth(100); 78 textview.setSingleLine(); 79 textview.setEllipsize(TextUtils.TruncateAt.END); 80 textview.setText(content); 81 LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams( 82 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 83 addView(textview, layoutparams); 84 } 85 } 86 } 87 88 /** 89 * 重写onLayout,在layout每个元素之前,需要计算该元素是否需要折行到下一行显示 90 */ 91 protected void onLayout(boolean changed, int l, int t, int r, int b) { 92 int count = getChildCount(); 93 if (count == 0) { 94 super.onLayout(changed, l, count, r, b); 95 return; 96 } 97 int lengthX = 0; 98 int lengthY = 0; 99 for (int i = 0; i < count; i++) { 100 View child = getChildAt(i); 101 int width = child.getMeasuredWidth(); 102 int height = child.getMeasuredHeight(); 103 child.layout(lengthX, lengthY, lengthX + width, lengthY + height); 104 int right = lengthX + width + VIEW_MARGIN_HORIZONTAL; 105 int nextWidth = 0; 106 if (i < count - 1) { 107 nextWidth = getChildAt(i + 1).getMeasuredWidth(); 108 } 109 if (right + nextWidth > (r - l)) { 110 lengthX = 0; 111 lengthY += height + VIEW_MARGIN_VERTICAL; 112 } else { 113 lengthX = right; 114 } 115 } 116 } 117 118 @Override 119 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 120 int count = getChildCount(); 121 if (count == 0) { 122 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 123 return; 124 } 125 int lengthX = 0; 126 int lengthY = 0; 127 int r = MeasureSpec.getSize(widthMeasureSpec); 128 // 定义子View的宽度最大不能超过r宽度 129 int childWidth = MeasureSpec.makeMeasureSpec(r, MeasureSpec.AT_MOST); 130 for (int i = 0; i < getChildCount(); i++) { 131 View child = getChildAt(i); 132 child.measure(childWidth, MeasureSpec.UNSPECIFIED); 133 int width = child.getMeasuredWidth(); 134 int height = child.getMeasuredHeight(); 135 int right = lengthX + width + VIEW_MARGIN_HORIZONTAL; 136 int nextWidth = 0; 137 // 非最后一个元素需要尝试计算一下能否放的下 138 if (i < count - 1) { 139 nextWidth = getChildAt(i + 1).getMeasuredWidth(); 140 } else { 141 // 最后一个计算高度的时候要加最后一排的高度 142 lengthY += height; 143 } 144 if (right + nextWidth > r) { 145 // 如果放不下,换行 146 lengthX = 0; 147 lengthY += height + VIEW_MARGIN_VERTICAL; 148 } else { 149 // 如果能放下,往后加 150 lengthX = right; 151 } 152 } 153 setMeasuredDimension( 154 widthMeasureSpec, 155 MeasureSpec.makeMeasureSpec(lengthY, 156 MeasureSpec.getMode(heightMeasureSpec))); 157 } 158 159 @Override 160 public void onClick(View view) { 161 if (mHotwordsOnclickListener != null) { 162 mHotwordsOnclickListener.hotwordOnclick((TextView) view); 163 } 164 } 165 }
时间: 2024-11-13 08:48:08