我需要修复多项选择项目。当我选择其中一个项目时,它会改变它的颜色。但是当我再次打开左侧抽屉时,它仍然被选中。在2种情况下,我需要丢弃任何选定的项目:1)当我按下另一个项目时,所选项目必须取消选中。 2)当左侧抽屉关闭时,选择必须丢弃。
[Activity(Theme = "@style/GsiTheme", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainPageActivity : AppCompatActivity, IActivityWithToolbar
{
#region Views
[InjectView(Resource.Id.mainpageToolbar)]
SupportToolbar _toolbar;
[InjectView(Resource.Id.navigation_view)]
NavigationView _navigationView;
[InjectView(Resource.Id.drawer)]
DrawerLayout _drawer;
#endregion
ActionBarDrawerToggle _drawerToggle;
public SupportToolbar Toolbar
{
get
{
return _toolbar;
}
}
Dictionary<int, Type> _fragmentTypesById = new Dictionary<int, Type>() {
{Resource.Id.categoriesMenuItem,typeof(CategoriesListFragment)},
{Resource.Id.opportunitiesMenuItem,typeof(OpportunitiesListFragment)},
{Resource.Id.prospectsMenuItem,typeof(ProspectsListFragment)},
{Resource.Id.suppliersMenuItem,typeof(SuppliersListFragment)},
{Resource.Id.accountsMenuItem,typeof(AccountsListFragment)},
{Resource.Id.fabricantsMenuItem,typeof(ManufacturersListfragment)},
{Resource.Id.contactsMenuItem,typeof(ContactsListFragment)},
{Resource.Id.productsMenuItem,typeof(ProductsListFragment)},
{Resource.Id.warehouseMenuItem,typeof(WarehouseListFragment)},
{Resource.Id.stockMenuItem,typeof(StocksListFragment)}
};
int _selectedPageMenuId;
protected override void OnCreate(Bundle savedInstanceState)
{
try
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.mainPagelayout);
Cheeseknife.Inject(this);
initUi();
//var ft = this.SupportFragmentManager.BeginTransaction();
//ft.Add(Resource.Id.mainPageFragmentHolder, new CategoriesListFragment());
//ft.Commit();
}
catch (Exception ex)
{
Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
}
}
private void initFragmentByMenuId(int menuId)
{
if (_selectedPageMenuId == menuId)
return;
if (!_fragmentTypesById.ContainsKey(menuId))
return;
var currFragment = Activator.CreateInstance(_fragmentTypesById[menuId]) as Android.Support.V4.App.Fragment;
Android.Support.V4.App.FragmentTransaction ft = SupportFragmentManager.BeginTransaction();
ft.Replace(Resource.Id.mainPageFragmentHolder, currFragment, currFragment.GetType().Name);
ft.Commit();
_selectedPageMenuId = menuId;
}
void navigationItem_selected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
_navigationView.Menu.FindItem(e.MenuItem.ItemId).SetChecked(true);
initFragmentByMenuId(e.MenuItem.ItemId);
_drawer.CloseDrawers();
}
void subscribeEvents()
{
_navigationView.NavigationItemSelected += navigationItem_selected;
}
void unSubscribeEvents()
{
_navigationView.NavigationItemSelected -= navigationItem_selected;
}
void initUi()
{
SetSupportActionBar(_toolbar);
SupportActionBar.Title = "Dashboard";
_drawerToggle = new CustActionBarDrawToggle(this, _drawer, 0, 0);
_drawer.AddDrawerListener(_drawerToggle);
_drawerToggle.SyncState();
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
_drawerToggle.OnOptionsItemSelected(item);
return base.OnOptionsItemSelected(item);
}
protected override void OnStart()
{
try
{
base.OnStart();
subscribeEvents();
}
catch (Exception ex)
{
Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
try
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);
}
catch (Exception ex)
{
Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
}
}
protected override void OnStop()
{
try
{
base.OnStop();
unSubscribeEvents();
}
catch (Exception ex)
{
Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
}
}
}
}
答案 0 :(得分:0)
我认为您可以通过始终保存最后选择的项目并确保在选择新项目之前取消选择来轻松实现您想要的目标。
MenuItem lastSelected;
void navigationItem_selected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
// If another item is already selected
if(lastSelected != null) {
lastSelected.SetChecked(false);
}
// Save the handle to the new item and select it
lastSelected = _navigationView.Menu.FindItem(e.MenuItem.ItemId);
lastSelected.SetChecked(true);
initFragmentByMenuId(e.MenuItem.ItemId);
_drawer.CloseDrawers();
}
此外,您只需订阅DrawerClosed
事件并在其中执行相同的lastSelected.SetChecked(false)
。只有在抽屉完全关闭后才会调用DrawerClosed
。