Skip to content

Commit

Permalink
support highlight title
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingRadish committed Nov 24, 2016
1 parent 496a3aa commit f8f3656
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
29 changes: 28 additions & 1 deletion app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,6 +22,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand All @@ -28,6 +33,7 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
private List<Note> mData;
private Map<String, String> mNotebookId2TitleMaps;
private NoteAdapterListener mListener;
private Pattern mTitleHighlight;

public NoteAdapter(NoteAdapterListener listener) {
mListener = listener;
Expand All @@ -39,6 +45,14 @@ public void load(List<Note> source) {
notifyDataSetChanged();
}

public void setHighlight(String titleKeyWord) {
if (TextUtils.isEmpty(titleKeyWord)) {
mTitleHighlight = null;
} else {
mTitleHighlight = Pattern.compile(titleKeyWord, Pattern.CASE_INSENSITIVE);
}
}

@Override
public NoteAdapter.NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
Expand All @@ -60,7 +74,7 @@ public void onBindViewHolder(NoteAdapter.NoteHolder holder, int position) {
if (TextUtils.isEmpty(note.getTitle())) {
holder.titleTv.setText(R.string.untitled);
} else {
holder.titleTv.setText(note.getTitle());
holder.titleTv.setText(getHighlightedText(note.getTitle()));
}
holder.contentTv.setText(note.getContent());
holder.notebookTv.setText(mNotebookId2TitleMaps.get(note.getNoteBookId()));
Expand Down Expand Up @@ -97,6 +111,19 @@ public boolean onLongClick(View v) {
});
}

private CharSequence getHighlightedText(String text) {
if (mTitleHighlight == null) {
return text;
}
SpannableStringBuilder builder = new SpannableStringBuilder(text);
Matcher matcher = mTitleHighlight.matcher(text);
int color = 0xFFFDD835;
while (matcher.find()) {
builder.setSpan(new BackgroundColorSpan(color), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
}
return builder;
}

@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ private void searchTitle(String keyword) {
mNotes = NoteService.searchNoteWithTitle(keyword);
Collections.sort(mNotes, new Note.UpdateTimeComparetor());
}
mAdapter.setHighlight(keyword);
mAdapter.load(mNotes);
}

Expand Down

0 comments on commit f8f3656

Please sign in to comment.