====== Фильтр в заголовке ======
Итак надо сделать в заголовке активити кнопку для вводе текста для фильтра.
Сначала надо создать layout_search.xml для инпута:
===== layout_search.xml =====
Далее надо в menu.xml нужно добавить атрибут //android:actionLayout// и указать //android:showAsAction="always|collapseActionView"//, для остальных элементов меню надо указать //android:showAsAction="ifRoom"//
===== menu.xml =====
В //activity// или //fragment// в методе //onCreateOptionsMenu// что-то типа:
===== fragment.java =====
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
final EditText editText = (EditText) menu.findItem(
R.id.menu_search).getActionView();
editText.addTextChangedListener(textWatcher);
MenuItem menuItem = menu.findItem(R.id.menu_search);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
editText.clearFocus();
return true; // Return true to expand action view
}
});
}
и добавьте //textWatcherListener//
===== textWatcherListener =====
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (null != mFollowingAdapter) {
mAdapter.getFilter().filter(s);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
[[http://stackoverflow.com/questions/13560438/android-actionbar-sherlock-search-filter]]