Пример #1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    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);
    menu = navigationView.getMenu();

    // Load menu
    //   deleteDatabase("sub_reddit-db");
    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "sub_reddit-db", null);
    SQLiteDatabase db = helper.getWritableDatabase();
    DaoMaster daoMaster = new DaoMaster(db);
    DaoSession daoSession = daoMaster.newSession();
    SubRedditDao subRedditDao = daoSession.getSubRedditDao();
    menuList = subRedditDao.loadAll();
    if (menuList.size() == 0) { // init
      Log.i("MyTag", "Subreddit init - list was empty");
      subRedditDao.insert(new SubReddit(null, "art", "art"));
      subRedditDao.insert(new SubReddit(null, "belgium", "belgium"));
      subRedditDao.insert(new SubReddit(null, "gifs", "gifs"));
      subRedditDao.insert(new SubReddit(null, "funny", "funny"));
      subRedditDao.insert(new SubReddit(null, "pics", "pics"));
      subRedditDao.insert(new SubReddit(null, "diy", "diy"));
      menuList = subRedditDao.loadAll();
    }
    // Dynamicly load menu
    for (SubReddit item : menuList) {
      menu.add(R.id.menuSubreddit, menuList.indexOf(item), Menu.NONE, item.getTitle());
    }

    // Load fragment
    Bundle bundle = new Bundle();
    bundle.putLong("subRedditId", menuList.get(0).getId());
    Fragment fragment = new SubRedditListFragment();
    fragment.setArguments(bundle);
    getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.frameLayoutMain, fragment)
        .addToBackStack(null)
        .commit();
  }
Пример #2
0
  @SuppressWarnings("StatementWithEmptyBody")
  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
    SubReddit subreddit = menuList.get(item.getItemId());
    Log.i("MyTag", "Clicked on " + subreddit.getTitle());
    // Load fragment

    Bundle bundle = new Bundle();
    bundle.putLong("subRedditId", subreddit.getId());
    Fragment fragment = new SubRedditListFragment();
    fragment.setArguments(bundle);
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.frameLayoutMain, fragment)
        .addToBackStack(null)
        .commit();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
  }