我在HomeActivity中有一个导航抽屉。在导航中,我有一些片段,每个片段都包含不同的回收视图。我想在操作栏中实现搜索视图,以便它将根据活动片段在回收视图中搜索。
这是片段代码的:
public class HomeFragment extends Fragment {
private static BackEnd backEnd;
private RecyclerView rvBooks;
private BookAdapter adapter;
private MenuItem mSearchAction;
private SearchView searchView;
private ActionBar actionBar;
private boolean isSearchOpened = false;
private EditText etSearch;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
rvBooks = (RecyclerView) view.findViewById(R.id.rv_books);
rvBooks.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new BookAdapter(getActivity());
rvBooks.setAdapter(adapter);
try {
backEnd = BackEndFactory.getInstance(getActivity());
for(int i = 0; i < 10; i++)
{
backEnd.addBook(new Book(R.mipmap.ic_launcher, "Genere "+(i+1), "Book "+(i+1), "2015", "Author", 100));
}
adapter.setBookList(backEnd.getBooksList());
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.home, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_search:
handleMenuSearch();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mSearchAction = menu.findItem(R.id.action_search);
searchView = (SearchView) mSearchAction.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
adapter.clearData();
adapter.setBookList(backEnd.searchForBook(query));
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
private void handleMenuSearch() {
actionBar = getSupportActionBar(); //get the actionbar
if(isSearchOpened){ //test if the search is open
if(actionBar != null)
{
actionBar.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar
actionBar.setDisplayShowTitleEnabled(true); //show the title in the action bar
}
// hides the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
// add the search icon in the action bar
mSearchAction.setIcon(getResources().getDrawable(R.drawable.ic_search));
isSearchOpened = false;
} else { // open the search entry
if(actionBar != null)
{
actionBar.setDisplayShowCustomEnabled(true); //enable it to display a
// custom view in the action bar.
// action.setCustomView(R.layout.search_bar);//add the custom view
actionBar.setDisplayShowTitleEnabled(false); //hide the title
}
// open the keyboard focused in the edtSearch
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT);
// add the close icon
mSearchAction.setIcon(getResources().getDrawable(R.drawable.ic_search));
isSearchOpened = true;
}
}
}
我在handleMenuSearch()
功能方面有问题: 无法识别getSupportActionBar()
和getSystemService(Context.INPUT_METHOD_SERVICE)
,因为片段不是活动。
那么,请如何正确操作?
您需要
通过调用getActivity()
来获取实际的父上下文(即父活动(,如下所示:
getActivity().getSupportActionBar();
...
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
...