private void ExitSearchUi()
        {
            searchView.Text = String.Empty;

            if (mSearchFragment != null)
            {
                mSearchFragment.UserVisibleHint = false;

                var transaction = SupportFragmentManager
                                  .BeginTransaction();
                transaction.Remove(mSearchFragment);
                mSearchFragment = null;
                transaction.CommitAllowingStateLoss();
            }

            HideInputMethod(CurrentFocus);
            searchView?.ClearFocus();
            mActionMode?.Finish();
            //locSpinner?.Parent?.RequestFocus();
            inSearchUI = false;

            // Reset to last selected item
            if (query_vm == null && selectedItem != null)
            {
                locSpinner.SetSelection(locAdapter.GetPosition(selectedItem));
            }

            // Set scroll flag
            var toolbarParams = collapsingToolbar.LayoutParameters as AppBarLayout.LayoutParams;

            toolbarParams.ScrollFlags &= AppBarLayout.LayoutParams.ScrollFlagScroll;
        }
 public override void OnAttachFragment(Android.Support.V4.App.Fragment fragment)
 {
     if (fragment is LocationSearchFragment locationSearchFragment)
     {
         mSearchFragment = locationSearchFragment;
         SetupSearchUi();
     }
 }
        private void AddSearchFragment()
        {
            if (mSearchFragment != null)
            {
                return;
            }

            var ft             = SupportFragmentManager.BeginTransaction();
            var searchFragment = new LocationSearchFragment();

            searchFragment.SetClickListener((object sender, RecyclerClickEventArgs e) =>
            {
                var adapter = sender as LocationQueryAdapter;
                var v       = (LocationQuery)e.View;

                if (!String.IsNullOrEmpty(adapter.Dataset[e.Position].LocationQuery))
                {
                    query_vm = adapter.Dataset[e.Position];
                }
                else
                {
                    query_vm = null;
                }

                if (String.IsNullOrWhiteSpace(query_vm.LocationQuery))
                {
                    // Stop since there is no valid query
                    query_vm = null;
                    return;
                }

                // Cancel other tasks
                mSearchFragment.CtsCancel();

                ShowLoading(true);

                if (mSearchFragment.CtsCancelRequested())
                {
                    ShowLoading(false);
                    query_vm = null;
                    return;
                }

                // Check if location already exists
                if (Favorites.FirstOrDefault(l => l.query == query_vm.LocationQuery) is LocationData loc)
                {
                    ShowLoading(false);
                    ExitSearchUi();

                    // Set selection
                    query_vm = null;
                    locSpinner.SetSelection(
                        locAdapter.GetPosition(new ComboBoxItem(loc.name, loc.query)));
                    return;
                }

                if (mSearchFragment.CtsCancelRequested())
                {
                    ShowLoading(false);
                    query_vm = null;
                    return;
                }

                // We got our data so disable controls just in case
                adapter.Dataset.Clear();
                adapter.NotifyDataSetChanged();
                searchFragment.View.FindViewById <RecyclerView>(Resource.Id.recycler_view).Enabled = false;

                // Save data
                var item = new ComboBoxItem(query_vm.LocationName, query_vm.LocationQuery);
                var idx  = locAdapter.Count - 1;
                locAdapter.Insert(item, idx);
                locSpinner.SetSelection(idx);
                locSummary.Text = item.Display;

                // Hide dialog
                ShowLoading(false);
                ExitSearchUi();
            });
            searchFragment.UserVisibleHint = false;
            ft.Add(Resource.Id.search_fragment_container, searchFragment);
            ft.CommitAllowingStateLoss();
        }