Answer the question
In order to leave comments, you need to log in
Working with one searchview from different fragments?
There is a mainActivity with a NavigationDrawer and a Toolbar.
Activiti switches different fragments.
Many fragments should be searchable, some should not.
SearchView moved to the toolbar.
In order not to prescribe my search for each fragment, I put it all in onCreateOptionsMenu mainActivity.
Question: how and where should I hide and show the SearchView when changing fragments and how to call the method of the current fragment when confirming the search?
To make it clearer, I'll post some code:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
Log.d(LOG_TAG, "onCreateOptionsMenu");
getMenuInflater().inflate(R.menu.menu_main, menu);
connectionStatus = menu.findItem(R.id.connectionStatus);
searchItem = menu.findItem( R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
// make blinking cursor
final EditText searchTextView = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
} catch (Exception ignored) {}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if( ! searchView.isIconified()) {
//todo call current fragment's method
searchView.setIconified(true);
}
searchItem.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
//hide overflow menu button when expanded
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
setItemsVisibility(menu, searchItem, true);
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
setItemsVisibility(menu, searchItem, false);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
drawerLayout.closeDrawers();
Fragment fragment = null;
switch (menuItem.getItemId()) {
case R.id.map:
fragment = new Fragment_Map();
//show search
break;
case R.id.subscribers:
fragment = new Fragment_Subscribers();
//show disable search
break;
case R.id.customlists:
fragment = new Fragment_AccessUserlists();
break;
case R.id.entities:
break;
case R.id.layers:
fragment = new Fragment_Layers();
break;
case R.id.messages:
break;
case R.id.profile:
break;
case R.id.settings:
break;
case R.id.logout:
App_WeOnMap.getAuthenticator().doLogout();
Intent intent = new Intent(Activity_Main.this, Activity_Login.class);
startActivity(intent);
finish();
break;
case R.id.closeapp:
finish();
break;
default:
break;
}
if (fragment != null) {
fragment.setRetainInstance(true);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, fragment).commit();
}
return true;
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question