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最终要显示