From 92555ec70e976faa05bec80b91590297136d03d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E4=BB=BB=E5=BD=A6?= Date: Fri, 16 Nov 2018 08:56:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=BE=E7=BD=AE=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E6=98=BE=E7=A4=BA=E8=A1=8C=E6=95=B0(maxLines)?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../labelsviewdemo/MainActivity.java | 3 ++ .../com/donkingliang/labels/LabelsView.java | 35 +++++++++++++++++++ labels/src/main/res/values/attr.xml | 1 + 3 files changed, 39 insertions(+) diff --git a/app/src/main/java/com/donkingliang/labelsviewdemo/MainActivity.java b/app/src/main/java/com/donkingliang/labelsviewdemo/MainActivity.java index 0275ded..b002f0f 100644 --- a/app/src/main/java/com/donkingliang/labelsviewdemo/MainActivity.java +++ b/app/src/main/java/com/donkingliang/labelsviewdemo/MainActivity.java @@ -61,6 +61,9 @@ public CharSequence getLabelText(TextView label, int position, TestBean data) { } }); + // 设置最大显示行数,小于等于0则不限行数。 +// labelsView.setMaxLines(1); + findViewById(R.id.btn_none).setOnClickListener(this); findViewById(R.id.btn_single).setOnClickListener(this); findViewById(R.id.btn_multi).setOnClickListener(this); diff --git a/labels/src/main/java/com/donkingliang/labels/LabelsView.java b/labels/src/main/java/com/donkingliang/labels/LabelsView.java index 35acf44..019e8f7 100644 --- a/labels/src/main/java/com/donkingliang/labels/LabelsView.java +++ b/labels/src/main/java/com/donkingliang/labels/LabelsView.java @@ -32,6 +32,7 @@ public class LabelsView extends ViewGroup implements View.OnClickListener { private int mLineMargin; private SelectType mSelectType; private int mMaxSelect; + private int mMaxLines; //用于保存label数据的key private static final int KEY_DATA = R.id.tag_key_data; @@ -106,6 +107,7 @@ private void getAttrs(Context context, AttributeSet attrs) { mSelectType = SelectType.get(type); mMaxSelect = mTypedArray.getInteger(R.styleable.labels_view_maxSelect, 0); + mMaxLines = mTypedArray.getInteger(R.styleable.labels_view_maxLines, 0); mTextColor = mTypedArray.getColorStateList(R.styleable.labels_view_labelTextColor); mTextSize = mTypedArray.getDimension(R.styleable.labels_view_labelTextSize, sp2px(context, 14)); @@ -141,6 +143,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int maxLineWidth = 0; //记录最宽的行宽 int maxItemHeight = 0; //记录一行中item高度最大的高度 boolean begin = true; //是否是行的开头 + int lineCount = 1; for (int i = 0; i < count; i++) { View view = getChildAt(i); @@ -153,6 +156,10 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { } if (maxWidth <= lineWidth + view.getMeasuredWidth()) { + lineCount++; + if (mMaxLines > 0 && lineCount > mMaxLines) { + break; + } contentHeight += mLineMargin; contentHeight += maxItemHeight; maxItemHeight = 0; @@ -214,12 +221,17 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto int contentWidth = right - left; int maxItemHeight = 0; + int lineCount = 1; int count = getChildCount(); for (int i = 0; i < count; i++) { View view = getChildAt(i); if (contentWidth < x + view.getMeasuredWidth() + getPaddingRight()) { + lineCount++; + if (mMaxLines > 0 && lineCount > mMaxLines) { + break; + } x = getPaddingLeft(); y += mLineMargin; y += maxItemHeight; @@ -242,6 +254,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto private static final String KEY_LINE_MARGIN_STATE = "key_line_margin_state"; private static final String KEY_SELECT_TYPE_STATE = "key_select_type_state"; private static final String KEY_MAX_SELECT_STATE = "key_max_select_state"; + private static final String KEY_MAX_LINES_STATE = "key_max_lines_state"; // 由于新版(1.4.0)的标签列表允许设置任何类型的数据,而不仅仅是String。并且标签显示的内容 // 最终由LabelTextProvider提供,所以LabelsView不再在onSaveInstanceState()和onRestoreInstanceState() // 中保存和恢复标签列表的数据。 @@ -274,6 +287,9 @@ protected Parcelable onSaveInstanceState() { bundle.putInt(KEY_SELECT_TYPE_STATE, mSelectType.value); //保存标签的最大选择数量 bundle.putInt(KEY_MAX_SELECT_STATE, mMaxSelect); + //保存标签的最大行数 + bundle.putInt(KEY_MAX_LINES_STATE, mMaxLines); + //保存标签列表 // if (!mLabels.isEmpty()) { // bundle.putStringArrayList(KEY_LABELS_STATE, mLabels); @@ -323,6 +339,9 @@ protected void onRestoreInstanceState(Parcelable state) { setSelectType(SelectType.get(bundle.getInt(KEY_SELECT_TYPE_STATE, mSelectType.value))); //恢复标签的最大选择数量 setMaxSelect(bundle.getInt(KEY_MAX_SELECT_STATE, mMaxSelect)); + //恢复标签的最大行数 + setMaxLines(bundle.getInt(KEY_MAX_LINES_STATE, mMaxLines)); + // //恢复标签列表 // ArrayList labels = bundle.getStringArrayList(KEY_LABELS_STATE); // if (labels != null && !labels.isEmpty()) { @@ -818,6 +837,22 @@ public int getMaxSelect() { return mMaxSelect; } + /** + * 设置最大行数,小于等于0则不限行数。 + * + * @param maxLines + */ + public void setMaxLines(int maxLines) { + if (mMaxLines != maxLines) { + mMaxLines = maxLines; + requestLayout(); + } + } + + public int getMaxLines() { + return mMaxLines; + } + /** * 设置标签的点击监听 * diff --git a/labels/src/main/res/values/attr.xml b/labels/src/main/res/values/attr.xml index e0bc4ea..76abe4e 100644 --- a/labels/src/main/res/values/attr.xml +++ b/labels/src/main/res/values/attr.xml @@ -8,6 +8,7 @@ +