Пример #1
0
 /**
  * base on Toolbar get the TitleView by reflect way
  *
  * @param obj Toolbar
  * @return the title text view in Toolbar
  */
 public static TextView getTitleViewInToolbar(Toolbar obj) {
   TextView textView = null;
   try {
     Field title = obj.getClass().getDeclaredField("mTitleTextView");
     title.setAccessible(true);
     textView = (TextView) title.get(obj);
   } catch (NoSuchFieldException e) {
     e.printStackTrace();
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   }
   return textView;
 }
Пример #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // how to adjust the height of toolbar
    // http://stackoverflow.com/questions/17439683/how-to-change-action-bar-size
    // zsmth_actionbar_size @ dimen ==> ThemeOverlay.ActionBar @ styles ==> theme @ app_bar_main.xml

    // init floating action button & circular action menu
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    initCircularActionMenu(fab);

    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    mToggle =
        new ActionBarDrawerToggle(
            this,
            mDrawer,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    mDrawer.addDrawerListener(mToggle);
    mToggle.syncState();

    mNavigationView = (NavigationView) findViewById(R.id.nav_view);
    mNavigationView.setNavigationItemSelectedListener(this);
    mNavigationView.setCheckedItem(R.id.nav_guidance);

    // http://stackoverflow.com/questions/33161345/android-support-v23-1-0-update-breaks-navigationview-get-find-header
    View headerView = mNavigationView.getHeaderView(0);
    mAvatar = (WrapContentDraweeView) headerView.findViewById(R.id.nav_user_avatar);
    mAvatar.setOnClickListener(this);

    mUsername = (TextView) headerView.findViewById(R.id.nav_user_name);
    mUsername.setOnClickListener(this);

    // http://stackoverflow.com/questions/27097126/marquee-title-in-toolbar-actionbar-in-android-with-lollipop-sdk
    TextView titleTextView = null;
    try {
      Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
      f.setAccessible(true);
      titleTextView = (TextView) f.get(toolbar);
      titleTextView.setEllipsize(TextUtils.TruncateAt.START);
    } catch (NoSuchFieldException e) {
    } catch (IllegalAccessException e) {
    }

    // init all fragments
    initFragments();

    FragmentManager fm = getSupportFragmentManager();
    if (Settings.getInstance().isLaunchHotTopic()) {
      fm.beginTransaction().replace(R.id.content_frame, hotTopicFragment).commit();
    } else {
      fm.beginTransaction().replace(R.id.content_frame, favoriteBoardFragment).commit();
    }

    getSupportFragmentManager()
        .addOnBackStackChangedListener(
            new FragmentManager.OnBackStackChangedListener() {
              public void onBackStackChanged() {
                // Enable Up button only  if there are entries in the back stack
                boolean canback = getSupportFragmentManager().getBackStackEntryCount() > 0;
                if (canback) {
                  mToggle.setDrawerIndicatorEnabled(false);
                  getSupportActionBar().setDisplayShowHomeEnabled(true);
                  getSupportActionBar().setHomeButtonEnabled(true);
                  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                } else {
                  getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                  mToggle.setDrawerIndicatorEnabled(true);
                  mDrawer.addDrawerListener(mToggle);
                }
              }
            });

    // start service to maintain user status
    setupUserStatusReceiver();
    updateUserStatusNow();
    UpdateNavigationViewHeader();

    // schedule the periodical run
    MaintainUserStatusService.schedule(MainActivity.this, mReceiver);

    if (Settings.getInstance().isFirstRun()) {
      // show info dialog after 5 seconds for the first run
      final Handler handler = new Handler();
      handler.postDelayed(
          new Runnable() {
            @Override
            public void run() {
              showInfoDialog();
            }
          },
          2000);
    }
  }