Exemplo n.º 1
0
        public void OnItemSelected(int position)
        {
            Fragment        current            = FragmentManager.FindFragmentById(Resource.Id.fragment_container);
            Type            targetFragmentType = null;
            WeatherListType weatherListType    = 0;

            switch (mNavDrawerAdapter?.GetStringId(position))
            {
            case Resource.String.label_condition:
            default:
                targetFragmentType = typeof(WeatherNowFragment);
                break;

            case Resource.String.title_fragment_alerts:
                targetFragmentType = typeof(WeatherListFragment);
                weatherListType    = WeatherListType.Alerts;
                break;

            case Resource.String.label_forecast:
                targetFragmentType = typeof(WeatherListFragment);
                weatherListType    = WeatherListType.Forecast;
                break;

            case Resource.String.label_hourlyforecast:
                targetFragmentType = typeof(WeatherListFragment);
                weatherListType    = WeatherListType.HourlyForecast;
                break;

            case Resource.String.label_details:
                targetFragmentType = typeof(WeatherDetailsFragment);
                break;
            }

            if (typeof(WeatherNowFragment).Equals(targetFragmentType))
            {
                if (!Class.FromType(typeof(WeatherNowFragment)).Equals(current.Class))
                {
                    // Pop all since we're going home
                    FragmentManager.PopBackStackImmediate(null, PopBackStackFlags.Inclusive);
                }
            }
            else if (typeof(WeatherListFragment).Equals(targetFragmentType))
            {
                if (!Class.FromType(targetFragmentType).Equals(current.Class))
                {
                    // Add fragment to backstack
                    var ft = FragmentManager.BeginTransaction();
                    ft.Add(Resource.Id.fragment_container,
                           WeatherListFragment.NewInstance(weatherListType, mNavDrawerAdapter.WeatherNowView),
                           null)
                    .AddToBackStack(null);

                    if (FragmentManager.BackStackEntryCount > 0)
                    {
                        ft.Remove(current);
                    }

                    ft.Commit();
                }
                else if (current is WeatherListFragment forecastFragment)
                {
                    if (forecastFragment.Arguments != null &&
                        ((WeatherListType)forecastFragment.Arguments.GetInt("WeatherListType", 0)) != weatherListType)
                    {
                        Bundle args = new Bundle();
                        args.PutInt("WeatherListType", (int)weatherListType);
                        forecastFragment.Arguments = args;
                        forecastFragment.Initialize();
                    }
                }
            }
            else if (typeof(WeatherDetailsFragment).Equals(targetFragmentType))
            {
                if (!Class.FromType(typeof(WeatherDetailsFragment)).Equals(current.Class))
                {
                    // Add fragment to backstack
                    var ft = FragmentManager.BeginTransaction();
                    ft.Add(Resource.Id.fragment_container, WeatherDetailsFragment.NewInstance(mNavDrawerAdapter.WeatherNowView), null)
                    .AddToBackStack(null);

                    if (FragmentManager.BackStackEntryCount > 0)
                    {
                        ft.Remove(current);
                    }

                    ft.Commit();
                }
            }
        }