HASH TAG 사용하기
2016. 6. 28. 12:37ㆍ개발일기/스타일판다
요새 SNS들을 보면 해시태그(#blurblurblur~)를 많이 사용하는 것을 볼 수가 있다.
나 역시 이번 앱에 해시태그를 적용해 보기로 한다.
우선 cliackablespan을 상속 받는 클래스를 선언한다. (각각의 hash tag마다 클릭을 가능하게 하기 위함)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Hashtag extends ClickableSpan { | |
public interface ClickEventListener{ | |
void onClickEvent(String data); | |
} | |
private ClickEventListener mClickEventListener = null; | |
private Context context; | |
private TextPaint textPaint; | |
public Hashtag(Context ctx){ | |
super(); | |
context = ctx; | |
} | |
public void setOnClickEventListener(ClickEventListener listener){ | |
mClickEventListener = listener; | |
} | |
@Override | |
public void updateDrawState(TextPaint ds) { | |
textPaint = ds; | |
ds.setColor(ds.linkColor); | |
ds.setARGB(255, 30, 144, 255); | |
} | |
@Override | |
public void onClick(View widget) { | |
TextView tv = (TextView) widget; | |
Spanned s = (Spanned) tv.getText(); | |
int start = s.getSpanStart(this); | |
int end = s.getSpanEnd(this); | |
String theWord = s.subSequence(start + 1, end).toString(); | |
mClickEventListener.onClickEvent(theWord); | |
} | |
} |
해당 클래스에 listener를 달아서 클릭했을 시, 해당 listener로 클릭한 단어를 보내주도록 구현하였다.
그럼 이제 이 클래스를 이용해서 hash tag를 어떻게 쓰는지 살펴보자.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void setContent(){ | |
String tag = ""; | |
int i; | |
for(i = 0 ; i <mTagLists.size(); i++){ | |
tag += "#" + mTagLists.get(i) + " "; | |
} | |
ArrayList<int[]> hashtagSpans = getSpans(tag, '#'); | |
SpannableString tagsContent = new SpannableString(tag); | |
for(i = 0; i < hashtagSpans.size(); i++){ | |
int[] span = hashtagSpans.get(i); | |
int hashTagStart = span[0]; | |
int hashTagEnd = span[1]; | |
Hashtag hashTag = new Hashtag(this); | |
hashTag.setOnClickEventListener(new Hashtag.ClickEventListener() { | |
@Override | |
public void onClickEvent(String data) { | |
} | |
}); | |
tagsContent.setSpan(hashTag, | |
hashTagStart, | |
hashTagEnd, | |
0); | |
} | |
TextView tags_view = (TextView)findViewById(R.id.textview_tag); | |
if(tags_view != null) { | |
tags_view.setMovementMethod(LinkMovementMethod.getInstance()); | |
tags_view.setText(tagsContent); | |
} | |
} | |
public ArrayList<int[]> getSpans(String body, char prefix) { | |
ArrayList<int[]> spans = new ArrayList<int[]>(); | |
Pattern pattern = Pattern.compile(prefix + "\\w+"); | |
Matcher matcher = pattern.matcher(body); | |
// Check all occurrences | |
while (matcher.find()) { | |
int[] currentSpan = new int[2]; | |
currentSpan[0] = matcher.start(); | |
currentSpan[1] = matcher.end(); | |
spans.add(currentSpan); | |
} | |
return spans; | |
} |
간략하게 함수 기능을 요약하자면, #문자를 가진 단어를 가져와서 해당 단어의 start와 end를 각각 저장한다.
이렇게 저장된 tagStart와 tagEnd를 가지고 SpannableString 객체를 만든 후 그 결과 콘텐츠를 textView에 뿌려줌으로써 해당 함수의 기능은 완료된다.
아래의 그림과 같이 해당 기능이 잘 동작 됨을 확인할 수 있다.