Skip to content

Commit

Permalink
添加设置最大显示行数(maxLines)的功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
梁任彦 committed Nov 16, 2018
1 parent e84ccc6 commit 92555ec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions labels/src/main/java/com/donkingliang/labels/LabelsView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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()
// 中保存和恢复标签列表的数据。
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String> labels = bundle.getStringArrayList(KEY_LABELS_STATE);
// if (labels != null && !labels.isEmpty()) {
Expand Down Expand Up @@ -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;
}

/**
* 设置标签的点击监听
*
Expand Down
1 change: 1 addition & 0 deletions labels/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<enum name="MULTI" value="4" />
</attr>
<attr name="maxSelect" format="integer" />
<attr name="maxLines" format="integer" />
<attr name="labelTextColor" format="reference|color" />
<attr name="labelTextSize" format="dimension" />
<attr name="labelTextPaddingLeft" format="dimension" />
Expand Down

0 comments on commit 92555ec

Please sign in to comment.