Search in sources :

Example 1 with MainPresenterImpl

use of com.looklook.xinghongfei.looklook.presenter.implPresenter.MainPresenterImpl in project LookLook by xinghongfei.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    ButterKnife.bind(this);
    setSupportActionBar(toolbar);
    IMainPresenter = new MainPresenterImpl(this, this);
    IMainPresenter.getBackground();
    toolbar.setOnMenuItemClickListener(onMenuItemClick);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        animateToolbar();
    }
    addfragmentsAndTitle();
    //        setStatusColor();
    drawer.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
    if (savedInstanceState == null) {
        nevigationId = SharePreferenceUtil.getNevigationItem(this);
        if (nevigationId != -1) {
            currentMenuItem = navView.getMenu().findItem(nevigationId);
        }
        if (currentMenuItem == null) {
            currentMenuItem = navView.getMenu().findItem(R.id.zhihuitem);
        }
        if (currentMenuItem != null) {
            currentMenuItem.setChecked(true);
            // TODO: 16/8/17 add a fragment and set toolbar title
            Fragment fragment = getFragmentById(currentMenuItem.getItemId());
            String title = mTitleArryMap.get((Integer) currentMenuItem.getItemId());
            if (fragment != null) {
                switchFragment(fragment, title);
            }
        }
    } else {
        if (currentMenuItem != null) {
            Fragment fragment = getFragmentById(currentMenuItem.getItemId());
            String title = mTitleArryMap.get((Integer) currentMenuItem.getItemId());
            if (fragment != null) {
                switchFragment(fragment, title);
            }
        } else {
            switchFragment(new ZhihuFragment(), " ");
            currentMenuItem = navView.getMenu().findItem(R.id.zhihuitem);
        }
    }
    navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            if (currentMenuItem != item && currentMenuItem != null) {
                currentMenuItem.setChecked(false);
                int id = item.getItemId();
                SharePreferenceUtil.putNevigationItem(MainActivity.this, id);
                currentMenuItem = item;
                currentMenuItem.setChecked(true);
                switchFragment(getFragmentById(currentMenuItem.getItemId()), mTitleArryMap.get(currentMenuItem.getItemId()));
            }
            drawer.closeDrawer(GravityCompat.END, true);
            return true;
        }
    });
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        drawer.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {

            @Override
            public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                // inset the toolbar down by the status bar height
                ViewGroup.MarginLayoutParams lpToolbar = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
                lpToolbar.topMargin += insets.getSystemWindowInsetTop();
                lpToolbar.rightMargin += insets.getSystemWindowInsetRight();
                toolbar.setLayoutParams(lpToolbar);
                // inset the grid top by statusbar+toolbar & the bottom by the navbar (don't clip)
                mFragmentContainer.setPadding(mFragmentContainer.getPaddingLeft(), insets.getSystemWindowInsetTop() + ViewUtils.getActionBarSize(MainActivity.this), // landscape
                mFragmentContainer.getPaddingRight() + insets.getSystemWindowInsetRight(), mFragmentContainer.getPaddingBottom() + insets.getSystemWindowInsetBottom());
                // we place a background behind the status bar to combine with it's semi-transparent
                // color to get the desired appearance.  Set it's height to the status bar height
                View statusBarBackground = findViewById(R.id.status_bar_background);
                FrameLayout.LayoutParams lpStatus = (FrameLayout.LayoutParams) statusBarBackground.getLayoutParams();
                lpStatus.height = insets.getSystemWindowInsetTop();
                statusBarBackground.setLayoutParams(lpStatus);
                // inset the filters list for the status bar / navbar
                // need to set the padding end for landscape case
                // clear this listener so insets aren't re-applied
                drawer.setOnApplyWindowInsetsListener(null);
                return insets.consumeSystemWindowInsets();
            }
        });
    }
    int[][] state = new int[][] { // unchecked
    new int[] { -android.R.attr.state_checked }, // pressed
    new int[] { android.R.attr.state_checked } };
    int[] color = new int[] { Color.BLACK, Color.BLACK };
    int[] iconcolor = new int[] { Color.GRAY, Color.BLACK };
    navView.setItemTextColor(new ColorStateList(state, color));
    navView.setItemIconTintList(new ColorStateList(state, iconcolor));
    //主题变色
    MenuItem item = navView.getMenu().findItem(R.id.nav_theme);
    mThemeSwitch = (SwitchCompat) MenuItemCompat.getActionView(item).findViewById(R.id.view_switch);
    mThemeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mThemeSwitch.setChecked(isChecked);
            if (isChecked) {
                setThemeColor(Color.GREEN);
            } else {
                setThemeColor(getResources().getColor(R.color.colorPrimaryDark));
            }
        }
    });
}
Also used : NavigationView(android.support.design.widget.NavigationView) ViewGroup(android.view.ViewGroup) ColorStateList(android.content.res.ColorStateList) MenuItem(android.view.MenuItem) MainPresenterImpl(com.looklook.xinghongfei.looklook.presenter.implPresenter.MainPresenterImpl) MeiziFragment(com.looklook.xinghongfei.looklook.fragment.MeiziFragment) ZhihuFragment(com.looklook.xinghongfei.looklook.fragment.ZhihuFragment) TopNewsFragment(com.looklook.xinghongfei.looklook.fragment.TopNewsFragment) Fragment(android.support.v4.app.Fragment) NavigationView(android.support.design.widget.NavigationView) BindView(butterknife.BindView) View(android.view.View) ActionMenuView(android.support.v7.widget.ActionMenuView) TextView(android.widget.TextView) WindowInsets(android.view.WindowInsets) FrameLayout(android.widget.FrameLayout) ZhihuFragment(com.looklook.xinghongfei.looklook.fragment.ZhihuFragment) CompoundButton(android.widget.CompoundButton)

Aggregations

ColorStateList (android.content.res.ColorStateList)1 NavigationView (android.support.design.widget.NavigationView)1 Fragment (android.support.v4.app.Fragment)1 ActionMenuView (android.support.v7.widget.ActionMenuView)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 WindowInsets (android.view.WindowInsets)1 CompoundButton (android.widget.CompoundButton)1 FrameLayout (android.widget.FrameLayout)1 TextView (android.widget.TextView)1 BindView (butterknife.BindView)1 MeiziFragment (com.looklook.xinghongfei.looklook.fragment.MeiziFragment)1 TopNewsFragment (com.looklook.xinghongfei.looklook.fragment.TopNewsFragment)1 ZhihuFragment (com.looklook.xinghongfei.looklook.fragment.ZhihuFragment)1 MainPresenterImpl (com.looklook.xinghongfei.looklook.presenter.implPresenter.MainPresenterImpl)1