From b87bacf74a20ee0989c0405b80e2b8699b905064 Mon Sep 17 00:00:00 2001 From: teach Date: Sat, 12 Oct 2019 10:45:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=82=B9=E5=87=BB=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E6=94=B9=E5=8F=98=E9=80=89=E4=B8=AD=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E6=8B=A6=E6=88=AA=E4=BA=8B=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/donkingliang/labels/LabelsView.java | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/labels/src/main/java/com/donkingliang/labels/LabelsView.java b/labels/src/main/java/com/donkingliang/labels/LabelsView.java index 166fdb1..1fe6686 100644 --- a/labels/src/main/java/com/donkingliang/labels/LabelsView.java +++ b/labels/src/main/java/com/donkingliang/labels/LabelsView.java @@ -56,6 +56,7 @@ public class LabelsView extends ViewGroup implements View.OnClickListener { private OnLabelClickListener mLabelClickListener; private OnLabelSelectChangeListener mLabelSelectChangeListener; + private OnSelectChangeIntercept mOnSelectChangeIntercept; /** * Label的选择类型 @@ -567,16 +568,20 @@ public void onClick(View v) { boolean irrevocable = mSelectType == SelectType.MULTI && mCompulsorys.contains((Integer) label.getTag(KEY_POSITION)); irrevocable = irrevocable || (mSelectType == SelectType.MULTI && mSelectLabels.size() <= mMinSelect); irrevocable = irrevocable || mSelectType == SelectType.SINGLE_IRREVOCABLY; - if (!irrevocable) { + if (!irrevocable&& !selectChangeIntercept(label)) { setLabelSelect(label, false); } } else { if (mSelectType == SelectType.SINGLE || mSelectType == SelectType.SINGLE_IRREVOCABLY) { - innerClearAllSelect(); - setLabelSelect(label, true); + if (!selectChangeIntercept(label)) { + innerClearAllSelect(); + setLabelSelect(label, true); + } } else if (mSelectType == SelectType.MULTI && (mMaxSelect <= 0 || mMaxSelect > mSelectLabels.size())) { - setLabelSelect(label, true); + if (!selectChangeIntercept(label)) { + setLabelSelect(label, true); + } } } } @@ -603,6 +608,12 @@ private void setLabelSelect(TextView label, boolean isSelect) { } } + private boolean selectChangeIntercept(TextView label) { + return mOnSelectChangeIntercept != null && mOnSelectChangeIntercept.onIntercept(label, + label.getTag(KEY_DATA), label.isSelected(), !label.isSelected(), + (int) label.getTag(KEY_POSITION)); + } + /** * 取消所有选中的label */ @@ -1056,6 +1067,15 @@ public void setOnLabelSelectChangeListener(OnLabelSelectChangeListener l) { mLabelSelectChangeListener = l; } + /** + * 设置标签选中状态的点击改变拦截器 + * + * @param intercept + */ + public void setOnSelectChangeIntercept(OnSelectChangeIntercept intercept) { + mOnSelectChangeIntercept = intercept; + } + /** * sp转px */ @@ -1093,6 +1113,23 @@ public interface OnLabelSelectChangeListener { void onLabelSelectChange(TextView label, Object data, boolean isSelect, int position); } + /** + * 点击选中/取消选中时,拦截事件,返回true时,表示事件被拦截,不会改变标签的选中状态。 + * 当希望某个标签在特定条件下不被选中/取消选中时,可以使用事件拦截。 + * 只有用户点击改变标签选中状态时才会回调拦截,用其他方法改变时不会回调这个方法,不会被拦截。 + */ + public interface OnSelectChangeIntercept { + + /** + * @param label 标签 + * @param data 标签对应的数据 + * @param oldSelect 旧选中状态 + * @param newSelect 新选中状态 + * @param position 标签位置 + */ + boolean onIntercept(TextView label, Object data, boolean oldSelect, boolean newSelect, int position); + } + /** * 给标签提供最终需要显示的数据。因为LabelsView的列表可以设置任何类型的数据,而LabelsView里的每个item的是一 * 个TextView,只能显示CharSequence的数据,所以LabelTextProvider需要根据每个item的数据返回item最终要显示