使用 Fragments 在 ActionBar 中搜索视图
SearchView in ActionBar using Fragments
我在 HomeActivity 中有一个导航抽屉。
在导航中,我有每个包含不同 RecycleViews 的片段。我想在 ActionBar 中实现 SearchView,这样它会根据活动片段在 RecycleView 中搜索。
这是片段代码之一:
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()
来获取实际的父上下文(即父 Activity),如下所示:
getActivity().getSupportActionBar();
...
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
...
我在 HomeActivity 中有一个导航抽屉。 在导航中,我有每个包含不同 RecycleViews 的片段。我想在 ActionBar 中实现 SearchView,这样它会根据活动片段在 RecycleView 中搜索。
这是片段代码之一:
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()
来获取实际的父上下文(即父 Activity),如下所示:
getActivity().getSupportActionBar();
...
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
...