소스 다운받는곳
https://github.com/tlshenm/SearchToolbarTest
----------------------------------------------------------CODE-------------------------------------------------
SearchActivity
public class SearchActivity extends AppCompatActivity {
private NoticeRecyclerAdapter mAdapter;
private NoticeContent mNoticeContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void initView() {
getIntentDate();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.notice_search_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new NoticeRecyclerAdapter(this, mNoticeContent.getNoticeList()); //new NoticeRecyclerAdapter(Context, 당신의 데이터 리스트)
recyclerView.setAdapter(mAdapter);
}
private void getIntentDate(){
Intent intent = getIntent();
mNoticeContent =(NoticeContent)intent.getExtras().getSerializable(NoticeContent.NOTICE_CONTENT);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) findViewById(R.id.notice_search_view);
searchView.onActionViewExpanded(); //바로 검색 할 수 있도록
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(this.getComponentName()));
searchView.setQueryHint(getString(R.string.notice_search_hint));
queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
Log.i("onQueryTextChange", newText);
mAdapter.setFilter(filter(mNoticeContent.getNoticeList(), newText));
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
Log.i("onQueryTextSubmit", query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
}
return true;
}
private List<NoticeItem> filter(List<NoticeItem> noticeListItem, String query) {
query = query.toLowerCase();
final List<NoticeItem> filteredNoticeList = new ArrayList<>();
if (query != null && !query.equals("")) {
for (NoticeItem model : noticeListItem) {
final String title = model.getTitle().toLowerCase();
final String name = model.getName().toLowerCase();
if (title.contains(query)) {
filteredNoticeList.add(model);
}else if(name.contains(query)){
filteredNoticeList.add(model);
}
}
}
return filteredNoticeList;
}
}
---------------------------------------------------------------------------------------------------------------------
NoticeRecyclerAdapter
public class NoticeRecyclerAdapter extends RecyclerView.Adapter<NoticeRecyclerAdapter.ViewHolder> {
private final List<NoticeItem> mValues;
private Context mContext = null;
public NoticeRecyclerAdapter(Context context, List<NoticeItem> items) {
this.mContext = context;
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.notice_item, parent, false);
return new ViewHolder(view);
}
@Override
public int getItemCount() {
return mValues.size();
}
//이부분 중요!! 검색 리스트를 나오게하기 위해 꼭 필요
public void setFilter(List<NoticeItem> items) {
mValues.clear();
mValues.addAll(items);
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mItem = mValues.get(position);
holder.mTitleText.setText(mValues.get(position).getTitle());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mTitleText;
public NoticeItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mTitleText = (TextView) view.findViewById(R.id.notice_item_title);
}
}
}
----------------------------------------------------------XML---------------------------------------------------
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/notice_search_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="@color/white_color"
android:elevation="5dp"
app:contentInsetStartWithNavigation="0dp">
<android.support.v7.widget.SearchView
android:id="@+id/notice_search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/notice_search_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/notice_search_toolbar"/>
</RelativeLayout>