com.google.android.material.navigation.NavigationView类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(436)

本文整理了Java中com.google.android.material.navigation.NavigationView类的一些代码示例,展示了NavigationView类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NavigationView类的具体详情如下:
包路径:com.google.android.material.navigation.NavigationView
类名称:NavigationView

暂无

代码示例

代码示例来源:origin: LyndonChin/MasteringAndroidDataBinding

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Toolbar toolbar = findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  ActionBar actionbar = getSupportActionBar();
  actionbar.setDisplayHomeAsUpEnabled(true);
  actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
  mDrawerLayout = findViewById(R.id.drawer_layout);
  NavigationView navigationView = findViewById(R.id.nav_view);
  navigationView.setNavigationItemSelectedListener(
      new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
          // set item as selected to persist highlight
          menuItem.setChecked(true);
          // close drawer when item is tapped
          mDrawerLayout.closeDrawers();
          // Add code here to update the UI based on the item selected
          // For example, swap UI fragments here
          return true;
        }
      });
}

代码示例来源:origin: ankidroid/Anki-Android

mNavigationView.setNavigationItemSelectedListener(this);
Toolbar toolbar = mainView.findViewById(R.id.toolbar);
if (toolbar != null) {
View actionLayout = mNavigationView.getMenu().findItem(R.id.nav_night_mode).getActionView();
mNightModeSwitch = actionLayout.findViewById(R.id.switch_compat);
mNightModeSwitch.setChecked(preferences.getBoolean("invertedColors", false));

代码示例来源:origin: iTXTech/Daedalus

@Override
protected void onCreate(Bundle savedInstanceState) {
  if (Daedalus.isDarkTheme()) {
    setTheme(R.style.AppTheme_Dark_NoActionBar_TransparentStatusBar);
  }
  super.onCreate(savedInstanceState);
  instance = this;
  setContentView(R.layout.activity_main);
  Toolbar toolbar = findViewById(R.id.toolbar);
  //setSupportActionBar(toolbar); //causes toolbar issues
  DrawerLayout drawer = findViewById(R.id.main_drawer_layout);
  ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  drawer.addDrawerListener(toggle);
  toggle.syncState();
  NavigationView navigationView = findViewById(R.id.nav_view);
  navigationView.setNavigationItemSelectedListener(this);
  ((TextView) navigationView.getHeaderView(0).findViewById(R.id.textView_nav_version)).setText(getString(R.string.nav_version) + " " + BuildConfig.VERSION_NAME);
  ((TextView) navigationView.getHeaderView(0).findViewById(R.id.textView_nav_git_commit)).setText(getString(R.string.nav_git_commit) + " " + BuildConfig.GIT_COMMIT);
  updateUserInterface(getIntent());
}

代码示例来源:origin: gateship-one/odyssey

navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(navId);

代码示例来源:origin: ankidroid/Anki-Android

/** Sets selected navigation drawer item */
protected void selectNavigationItem(int itemId) {
  if (mNavigationView == null) {
    Timber.e("Could not select item in navigation drawer as NavigationView null");
    return;
  }
  Menu menu = mNavigationView.getMenu();
  if (itemId == -1) {
    for (int i = 0; i < menu.size(); i++) {
      menu.getItem(i).setChecked(false);
    }
  } else {
    MenuItem item = menu.findItem(itemId);
    if (item != null) {
      item.setChecked(true);
    } else {
      Timber.e("Could not find item %d", itemId);
    }
  }
}

代码示例来源:origin: codeka/wwmmo

navigationView.setNavigationItemSelectedListener(item -> {
final MenuItem empireMenuItem = navigationView.getMenu().findItem(R.id.nav_empire);
App.i.getServer().waitForHello(() -> App.i.getTaskRunner().runTask(() -> {
 String url = ImageHelper.getEmpireImageUrl(activity, EmpireManager.i.getMyEmpire(), 48, 48);
 navigationView.setTag(R.id.target_tag, target);
 Picasso.get().load(url).into(target);
}, Threads.UI));

代码示例来源:origin: KDE/kdeconnect-android

public void onDeviceSelected(String deviceId, boolean fromDeviceList) {
  mCurrentDevice = deviceId;
  preferences.edit().putString(STATE_SELECTED_DEVICE, deviceId).apply();
  if (mCurrentDevice != null) {
    mCurrentMenuEntry = deviceIdToMenuEntryId(deviceId);
    if (mCurrentMenuEntry == MENU_ENTRY_DEVICE_UNKNOWN) {
      uncheckAllMenuItems(mNavigationView.getMenu());
    } else {
      mNavigationView.setCheckedItem(mCurrentMenuEntry);
    }
    setContentFragment(DeviceFragment.newInstance(deviceId, fromDeviceList));
  } else {
    mCurrentMenuEntry = MENU_ENTRY_ADD_DEVICE;
    mNavigationView.setCheckedItem(mCurrentMenuEntry);
    setContentFragment(new PairingFragment());
  }
}

代码示例来源:origin: KDE/kdeconnect-android

View mDrawerHeader = mNavigationView.getHeaderView(0);
mNavViewDeviceName = mDrawerHeader.findViewById(R.id.device_name);
mNavigationView.setNavigationItemSelectedListener(menuItem -> {
  mCurrentMenuEntry = menuItem.getItemId();
  switch (mCurrentMenuEntry) {
mNavigationView.setCheckedItem(savedMenuEntry);

代码示例来源:origin: gotify/android

private void initDrawer() {
  setSupportActionBar(toolbar);
  navigationView.setItemIconTintList(null);
  ActionBarDrawerToggle toggle =
      new ActionBarDrawerToggle(
          this,
          drawer,
          toolbar,
          R.string.navigation_drawer_open,
          R.string.navigation_drawer_close);
  drawer.addDrawerListener(toggle);
  toggle.syncState();
  navigationView.setNavigationItemSelectedListener(this);
  View headerView = navigationView.getHeaderView(0);
  TextView header = headerView.findViewById(R.id.header_username);
  String host = HttpUrl.parse(settings.url()).host();
  header.setText(getString(R.string.connection, settings.user().getName(), host));
  TextView version = headerView.findViewById(R.id.header_versions);
  version.setText(getString(R.string.server_version, settings.serverVersion()));
  ImageButton refreshAll = headerView.findViewById(R.id.refresh_all);
  refreshAll.setOnClickListener(this::onRefreshAll);
}

代码示例来源:origin: vbier/habpanelviewer

navigationView.addHeaderView(navHeader);
navigationView.setNavigationItemSelectedListener(this);

代码示例来源:origin: morogoku/MTweaks-KernelAdiutorMOD

mNavigationView.setNavigationItemSelectedListener(this);
mNavigationView.setOnFocusChangeListener((v, hasFocus) -> {
  if (hasFocus) {
    v.clearFocus();

代码示例来源:origin: offensive-security/nethunter-app

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout navigationHeadView = (LinearLayout) inflater.inflate(R.layout.sidenav_header, null);
navigationView.addHeaderView(navigationHeadView);
titles.push(navigationView.getMenu().getItem(0).getTitle().toString());
  lastSelected = navigationView.getMenu().getItem(0);
  lastSelected.setChecked(true);

代码示例来源:origin: pranavpandey/dynamic-support

/**
 * Set menu for the navigation view.
 *
 * @param menuRes The menu resource id for the navigation view.
 */
public void setNavigationViewMenu(@MenuRes int menuRes) {
  mNavigationView.getMenu().clear();
  mNavigationView.inflateMenu(menuRes);
}

代码示例来源:origin: cbeyls/fosdem-companion-android

@Override
protected void onSaveInstanceState(Bundle outState) {
  // Ensure no fragment transaction attempt will occur after onSaveInstanceState()
  if (pendingNavigationMenuItem != null) {
    pendingNavigationMenuItem = null;
    if (currentSection != null) {
      navigationView.setCheckedItem(currentSection.getMenuItemId());
    }
  }
  super.onSaveInstanceState(outState);
}

代码示例来源:origin: pranavpandey/dynamic-support

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mDrawer = findViewById(R.id.ads_drawer_layout);
  mNavigationView = findViewById(R.id.ads_navigation_view);
  mNavHeaderIcon = mNavigationView.getHeaderView(0)
      .findViewById(R.id.ads_header_drawer_icon);
  mNavHeaderTitle = mNavigationView.getHeaderView(0)
      .findViewById(R.id.ads_header_drawer_title);
  mNavHeaderSubtitle = mNavigationView.getHeaderView(0)
      .findViewById(R.id.ads_header_drawer_subtitle);
  mDrawer.setDrawerElevation(DynamicUnitUtils.convertDpToPixels(8));
  setStatusBarColor(getStatusBarColor());
  setupDrawer();
}

代码示例来源:origin: cbeyls/fosdem-companion-android

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
  @Override
  public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
  navigationView.setCheckedItem(currentSection.getMenuItemId());

代码示例来源:origin: iTXTech/Daedalus

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
  menu = ((NavigationView) getActivity().findViewById(R.id.nav_view)).getMenu();
  toolbar = getActivity().findViewById(R.id.toolbar);
  toolbar.getMenu().clear();
  checkStatus();
}

代码示例来源:origin: morogoku/MTweaks-KernelAdiutorMOD

private void appendFragments(boolean setShortcuts) {
  mActualFragments.clear();
  Menu menu = mNavigationView.getMenu();
  menu.clear();
  SubMenu lastSubMenu = null;
  for (NavigationFragment navigationFragment : mFragments) {
    Class<? extends Fragment> fragmentClass = navigationFragment.mFragmentClass;
    int id = navigationFragment.mId;
    Drawable drawable = ContextCompat.getDrawable(this,
          AppSettings.isSectionIcons(this)
          && navigationFragment.mDrawable != 0 ? navigationFragment.mDrawable :
          R.drawable.ic_blank);
    if (fragmentClass == null) {
      lastSubMenu = menu.addSubMenu(id);
      mActualFragments.put(id, null);
    } else if (AppSettings.isFragmentEnabled(fragmentClass, this)) {
      MenuItem menuItem = lastSubMenu == null ? menu.add(0, id, 0, id) :
          lastSubMenu.add(0, id, 0, id);
      menuItem.setIcon(drawable);
      menuItem.setCheckable(true);
      if (mSelection != 0) {
        mNavigationView.setCheckedItem(mSelection);
      }
      mActualFragments.put(id, fragmentClass);
    }
  }
  if (setShortcuts) {
    setShortcuts();
  }
}

代码示例来源:origin: morogoku/MTweaks-KernelAdiutorMOD

private void onItemSelected(final int res, boolean saveOpened) {
  mDrawer.closeDrawer(GravityCompat.START);
  getSupportActionBar().setTitle(getString(res));
  mNavigationView.setCheckedItem(res);
  mSelection = res;
  final Fragment fragment = getFragment(res);
  if (saveOpened) {
    AppSettings.saveFragmentOpened(fragment.getClass(),
        AppSettings.getFragmentOpened(fragment.getClass(), this) + 1,
        this);
  }
  setShortcuts();
  mDrawer.postDelayed(()
          -> {
        Log.crashlyticsI("open " + fragment.getClass().getSimpleName());
        getSupportFragmentManager().beginTransaction().replace(
            R.id.content_frame, fragment, res + "_key").commitAllowingStateLoss();
      },
      250);
}

代码示例来源:origin: raphaelbussa/HeaderView

drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
headerView = navigationView.getHeaderView(0).findViewById(R.id.header_view);

相关文章