Skip to content

Commit c871367

Browse files
committed
Add listener for detect tag clicked.
1 parent e0ed7da commit c871367

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

library/src/main/java/me/gujun/android/taggroup/TagGroup.java

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import android.text.TextWatcher;
1818
import android.text.method.ArrowKeyMovementMethod;
1919
import android.util.AttributeSet;
20-
import android.util.Log;
2120
import android.util.TypedValue;
2221
import android.view.Gravity;
2322
import android.view.KeyEvent;
@@ -77,24 +76,34 @@ public class TagGroup extends ViewGroup {
7776

7877
/** The tag outline border color. */
7978
private int borderColor;
79+
8080
/** The tag text color. */
8181
private int textColor;
82+
8283
/** The tag background color. */
8384
private int backgroundColor;
85+
8486
/** The dash outline border color. */
8587
private int dashBorderColor;
88+
8689
/** The input tag hint text color. */
8790
private int inputHintColor;
91+
8892
/** The input tag type text color. */
8993
private int inputTextColor;
94+
9095
/** The checked tag outline border color. */
9196
private int checkedBorderColor;
97+
9298
/** The check text color */
9399
private int checkedTextColor;
94-
/** The checked markder color. */
100+
101+
/** The checked marker color. */
95102
private int checkedMarkerColor;
103+
96104
/** The checked tag background color. */
97105
private int checkedBackgroundColor;
106+
98107
/** The tag background color, when the tag is being pressed. */
99108
private int pressedBackgroundColor;
100109

@@ -119,8 +128,11 @@ public class TagGroup extends ViewGroup {
119128
/** Listener used to dispatch tag change event. */
120129
private OnTagChangeListener mOnTagChangeListener;
121130

131+
/** Listener used to dispatch tag click event. */
132+
private OnTagClickListener mOnTagClickListener;
133+
122134
/** Listener used to handle tag click event. */
123-
private OnTagClickListener mOnTagClickListener = new OnTagClickListener();
135+
private InternalTagClickListener mInternalTagClickListener = new InternalTagClickListener();
124136

125137
public TagGroup(Context context) {
126138
this(context, null);
@@ -464,7 +476,7 @@ protected void appendInputTag(String tag) {
464476
}
465477

466478
final TagView newInputTag = new TagView(getContext(), TagView.STATE_INPUT, tag);
467-
newInputTag.setOnClickListener(mOnTagClickListener);
479+
newInputTag.setOnClickListener(mInternalTagClickListener);
468480
addView(newInputTag);
469481
}
470482

@@ -475,7 +487,7 @@ protected void appendInputTag(String tag) {
475487
*/
476488
protected void appendTag(CharSequence tag) {
477489
final TagView newTag = new TagView(getContext(), TagView.STATE_NORMAL, tag);
478-
newTag.setOnClickListener(mOnTagClickListener);
490+
newTag.setOnClickListener(mInternalTagClickListener);
479491
addView(newTag);
480492
}
481493

@@ -513,6 +525,27 @@ public interface OnTagChangeListener {
513525
void onDelete(TagGroup tagGroup, String tag);
514526
}
515527

528+
/**
529+
* Register a callback to be invoked when a tag is clicked.
530+
*
531+
* @param l the callback that will run.
532+
*/
533+
public void setOnTagClickListener(OnTagClickListener l) {
534+
mOnTagClickListener = l;
535+
}
536+
537+
/**
538+
* Interface definition for a callback to be invoked when a tag is clicked.
539+
*/
540+
public interface OnTagClickListener {
541+
/**
542+
* Called when a tag has been clicked.
543+
*
544+
* @param tag The tag text of the tag that was clicked.
545+
*/
546+
void onTagClick(String tag);
547+
}
548+
516549
/**
517550
* Per-child layout information for layouts.
518551
*/
@@ -570,9 +603,9 @@ public void writeToParcel(Parcel dest, int flags) {
570603
}
571604

572605
/**
573-
* The tag view click listener.
606+
* The tag view click listener for internal use.
574607
*/
575-
class OnTagClickListener implements OnClickListener {
608+
class InternalTagClickListener implements OnClickListener {
576609
@Override
577610
public void onClick(View v) {
578611
final TagView tag = (TagView) v;
@@ -598,8 +631,9 @@ public void onClick(View v) {
598631
}
599632
}
600633
} else {
601-
// TODO Dispatch tag click event.
602-
Log.i("TagGroup", "Tag " + tag.getText() + " clicked.");
634+
if (mOnTagClickListener != null) {
635+
mOnTagClickListener.onTagClick(tag.getText().toString());
636+
}
603637
}
604638
}
605639
}
@@ -669,7 +703,7 @@ class TagView extends TextView {
669703
/** The path effect provide draw the dash border. */
670704
private PathEffect mPathEffect = new DashPathEffect(new float[]{10, 5}, 0);
671705

672-
public TagView(Context context, int state, CharSequence text) {
706+
public TagView(Context context, final int state, CharSequence text) {
673707
super(context);
674708
setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
675709
setLayoutParams(new TagGroup.LayoutParams(TagGroup.LayoutParams.WRAP_CONTENT,
@@ -687,6 +721,13 @@ public TagView(Context context, int state, CharSequence text) {
687721
setHint(state == STATE_INPUT ? inputHint : null);
688722
setMovementMethod(state == STATE_INPUT ? ArrowKeyMovementMethod.getInstance() : null);
689723

724+
setOnLongClickListener(new OnLongClickListener() {
725+
@Override
726+
public boolean onLongClick(View v) {
727+
return state != STATE_INPUT;
728+
}
729+
});
730+
690731
if (state == STATE_INPUT) {
691732
requestFocus();
692733

0 commit comments

Comments
 (0)