我想在应用程序启动时在导航抽屉中选择一个特定项目。
我在Visual Studio默认模板中添加了navigationView.SetCheckedItem(Resource.Id.nav_gallery);
。
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.Click += FabOnClick;
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.SetNavigationItemSelectedListener(this);
navigationView.SetCheckedItem(Resource.Id.nav_gallery);
}
但是不调用OnNavigationItemSelected
public bool OnNavigationItemSelected(IMenuItem item)
{
int id = item.ItemId;
if (id == Resource.Id.nav_camera)
{
// Handle the camera action
}
else if (id == Resource.Id.nav_gallery)
{
}
如果我单击导航项OnNavigationItemSelected
,则一切正常。
选择默认项目的正确方法是什么?
更新
我添加了这一行解决了它。点击特定的项目。
navigationView.Menu.PerformIdentifierAction(Resource.Id.nav_gallery, 0);
答案 0 :(得分:0)
navigationView.getMenu().getItem(position).setChecked(true);
您可以创建一个函数
public void selectDrawerItem(int position, String tag) {
switch (position) {
case 0:
fragment = new DashboardFragment();
tvToolbarTitleLand.setText("Dashboard");
break;
//code.................
}
if (fragment != null) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right);
transaction.replace(R.id.flMainContainer, fragment);
transaction.addToBackStack(tag);
transaction.commit();
}
navigationView.getMenu().getItem(position).setChecked(true);
}
如果您对此有任何疑问,请告诉我。
答案 1 :(得分:0)
public class BaseApp extends AppCompatActivity {
//define variables
protected String LOGTAG = "LOGDEBUG";
protected Toolbar toolbar;
protected NavigationView navigationView;
protected DrawerLayout drawerLayout;
private DateManager db = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//在启动时设置仪表板
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, new DashboardFragment());
fragmentTransaction.commit();
setNavDrawer();
}
//CREATE A NavDrawer() void
private void set NavDrawer(){
toolbar = (toolbar) findViewById(r.i.d.toolbar);
setsupportactionbar(toolbar);
navigationView.setNavigationItemSelected(memuItem)
{
if (menuItems.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
drawerlayout.closeDrawers();
//now checlk which items are being clicked
//并执行适当的操作
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.home:
DashboardFragment dashboardFragment = new DashboardFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, dashboardFragment,"DASHBOARD_FRAGMENT");
fragmentTransaction.commit();
return true;