protected NavigationView setupDrawer(int drawerLayoutIdToLoad) {
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    _mDrawerLayout = (DrawerLayout) findViewById(drawerLayoutIdToLoad);

    // In Android support 23.2.1, there is a bug where you can't inflate the menu in XML, must be
    // done manually
    navigationView.inflateMenu(R.menu.drawer_menu_items);
    navigationView.inflateHeaderView(R.layout.drawer_header);
    navigationView.setNavigationItemSelectedListener(
        new NavigationView.OnNavigationItemSelectedListener() {
          // This method will trigger on item Click of navigation menu
          @Override
          public boolean onNavigationItemSelected(MenuItem menuItem) {
            return selectDrawerItem(menuItem);
          }
        });
    _mDrawerToggle =
        new ActionBarDrawerToggle(
            this, // Host Activity
            _mDrawerLayout, // DrawerLayout object
            R.string.navigation_drawer_open, // Description for accessibility
            R.string.navigation_drawer_close // Description for accessibility
            );
    _mDrawerLayout.addDrawerListener(_mDrawerToggle);

    return navigationView;
  }
Example #2
0
  private void configViews() {

    // 设置显示Toolbar
    setSupportActionBar(mToolbar);

    // 设置Drawerlayout开关指示器,即Toolbar最左边的那个icon
    ActionBarDrawerToggle mActionBarDrawerToggle =
        new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close);
    mActionBarDrawerToggle.syncState();
    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

    // 给NavigationView填充顶部区域,也可在xml中使用app:headerLayout="@layout/header_nav"来设置
    mNavigationView.inflateHeaderView(R.layout.view_nav_header);
    // 给NavigationView填充Menu菜单,也可在xml中使用app:menu="@menu/menu_nav"来设置
    mNavigationView.inflateMenu(R.menu.menu_nav);
    // 自己写的方法,设置NavigationView中menu的item被选中后要执行的操作
    onNavgationViewMenuItemSelected(mNavigationView);

    // 初始化ViewPager的适配器,并设置给它
    pageAdapter = new CardPageAdapter(getSupportFragmentManager(), mTitles);
    mViewPager.setAdapter(pageAdapter);
    // 设置ViewPager最大缓存的页面个数
    mViewPager.setOffscreenPageLimit(5);
    // 给ViewPager添加页面动态监听器(为了让Toolbar中的Title可以变化相应的Tab的标题)
    mViewPager.addOnPageChangeListener(this);

    mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    // 将TabLayout和ViewPager进行关联,让两者联动起来
    mTabLayout.setupWithViewPager(mViewPager);
    // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
    mTabLayout.setTabsFromPagerAdapter(pageAdapter);

    // 设置FloatingActionButton的点击事件
    mFloatingActionButton.setOnClickListener(this);
  }
Example #3
0
  private void addFolderToNavView(Cursor cursor) {
    Menu menu = navigationView.getMenu();

    if (menu.size() != 0) Utils.removeAllMenuItems(menu);

    navigationView.inflateMenu(R.menu.activity_main_drawer);

    for (int i = 0; i < cursor.getCount(); i++) {
      Log.e(
          TAG,
          " "
              + cursor.getString(cursor.getColumnIndexOrThrow(Constants.FOLDER_NAME_COL))
              + ",  "
              + cursor.getString(cursor.getColumnIndexOrThrow(Constants.NOTES_COUNT_COL)));

      int id = cursor.getInt(cursor.getColumnIndexOrThrow(Constants.ID_COL));
      int order = 11;
      int count = cursor.getInt(cursor.getColumnIndexOrThrow(Constants.NOTES_COUNT_COL));
      if (id == myNotesNavId) order = 10;
      else if (id == trashNavId) {
        order = 10000;
        count = deletedCount;
      }

      addMenuCustomItem(
          menu,
          id,
          order,
          cursor.getString(cursor.getColumnIndexOrThrow(Constants.FOLDER_NAME_COL)),
          cursor.getInt(cursor.getColumnIndexOrThrow(Constants.FOLDER_ICON_COL)),
          count);
      cursor.moveToNext();
    }
    cursor.close();

    if (menu.findItem(folderId) == null) folderId = myNotesNavId;

    navigationView.setCheckedItem(folderId);

    attachFragment(Constants.FRAGMENT_LIST);
  }
Example #4
0
  private void populateTabs() {
    drawerList.getMenu().clear();
    drawerList.inflateMenu(R.menu.drawer_navigation);

    SharedPreferences prefs = Util.getPreferences(this);
    boolean podcastsEnabled = prefs.getBoolean(Constants.PREFERENCES_KEY_PODCASTS_ENABLED, true);
    boolean bookmarksEnabled =
        prefs.getBoolean(Constants.PREFERENCES_KEY_BOOKMARKS_ENABLED, true)
            && !Util.isOffline(this)
            && ServerInfo.canBookmark(this);
    boolean sharedEnabled =
        prefs.getBoolean(Constants.PREFERENCES_KEY_SHARED_ENABLED, true) && !Util.isOffline(this);
    boolean chatEnabled =
        prefs.getBoolean(Constants.PREFERENCES_KEY_CHAT_ENABLED, true) && !Util.isOffline(this);
    boolean adminEnabled =
        prefs.getBoolean(Constants.PREFERENCES_KEY_ADMIN_ENABLED, true) && !Util.isOffline(this);

    MenuItem offlineMenuItem = drawerList.getMenu().findItem(R.id.drawer_offline);
    if (Util.isOffline(this)) {
      setDrawerItemVisible(R.id.drawer_home, false);

      if (lastSelectedPosition == 0 || lastSelectedPosition == R.id.drawer_home) {
        String newFragment = Util.openToTab(this);
        if (newFragment == null || "Home".equals(newFragment)) {
          newFragment = "Artist";
        }

        lastSelectedPosition = getDrawerItemId(newFragment);
        drawerItemSelected(newFragment);
      }

      offlineMenuItem.setTitle(R.string.main_online);
    } else {
      offlineMenuItem.setTitle(R.string.main_offline);
    }

    if (!podcastsEnabled) {
      setDrawerItemVisible(R.id.drawer_podcasts, false);
    }
    if (!bookmarksEnabled) {
      setDrawerItemVisible(R.id.drawer_bookmarks, false);
    }
    if (!sharedEnabled) {
      setDrawerItemVisible(R.id.drawer_shares, false);
    }
    if (!chatEnabled) {
      setDrawerItemVisible(R.id.drawer_chat, false);
    }
    if (!adminEnabled) {
      setDrawerItemVisible(R.id.drawer_admin, false);
    }

    if (lastSelectedPosition != 0) {
      MenuItem item = drawerList.getMenu().findItem(lastSelectedPosition);
      if (item != null) {
        item.setChecked(true);
      }
    }
    drawerHeaderToggle.setImageResource(R.drawable.main_select_server_dark);

    showingTabs = true;
  }
 public void setNavigationMenu(@MenuRes final int id) {
   mNavigationView.getMenu().clear();
   mNavigationView.inflateMenu(id);
 }
Example #6
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    session = new UserSessionManager(getApplicationContext());
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle =
        new ActionBarDrawerToggle(
            this,
            drawer,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    View header = navigationView.getHeaderView(0);
    TextView text = (TextView) header.findViewById(R.id.user);

    if (session.isUserLoggedIn())
      text.setText(session.getUserDetails().get(UserSessionManager.KEY_EMAIL));
    ImageView image = (ImageView) header.findViewById(R.id.imageView);
    TextView textUser = (TextView) header.findViewById(R.id.user);
    if (session.isUserLoggedIn()) {
      navigationView.getMenu().clear();
      navigationView.inflateMenu(R.menu.activity_main_drawer);
      image.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              startActivity(new Intent(getApplicationContext(), UserProfile.class));
              drawer.closeDrawer(GravityCompat.START);
            }
          });
      textUser.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              startActivity(new Intent(getApplicationContext(), UserProfile.class));
              drawer.closeDrawer(GravityCompat.START);
            }
          });
    } else {
      navigationView.getMenu().clear();
      navigationView.inflateMenu(R.menu.activity_log_drawer);
    }

    ImageButton search = (ImageButton) findViewById(R.id.search);
    search.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), Search.class));
          }
        });
    ImageButton add = (ImageButton) findViewById(R.id.add);
    add.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), Add.class));
          }
        });

    // if(session.checkLogin())
    //   finish();

  }