com.vv.androidreview.ui.activites.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.vv.androidreview.ui.activites.MainActivity.java

Source

/*
 * Copyright (c) 2016. Vv <envyfan@qq.com><http://www.v-sounds.com/>
 *
 * This file is part of AndroidReview (Android??)
 *
 * AndroidReview is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  AndroidReview is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 * along with AndroidReview.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.vv.androidreview.ui.activites;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import com.vv.androidreview.R;
import com.vv.androidreview.base.BaseActivity;
import com.vv.androidreview.ui.fragment.Indicator;
import com.vv.androidreview.utils.DoubleClickExitHelper;
import com.vv.androidreview.utils.ToastHelper;

public class MainActivity extends BaseActivity {

    private DoubleClickExitHelper mDoubleClickExit;
    private FragmentTabHost mFragmentTabHost;
    protected TextView mCount;
    private View mRootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRootView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        setContentView(mRootView);

        initToolBar();
        initView();
        setStatusBarCompat();
    }

    private void initView() {
        //?TextView
        mCount = (TextView) findViewById(R.id.tv_count);
        mDoubleClickExit = new DoubleClickExitHelper(this);

        Indicator[] indicators = Indicator.values();
        mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mFragmentTabHost.setup(getApplicationContext(), getSupportFragmentManager(), R.id.realtabcontent);

        //?Tab
        for (int i = 0; i < indicators.length; i++) {
            TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(getString(indicators[i].getResName()));
            tabSpec.setIndicator(getIndicatorView(indicators[i]));
            mFragmentTabHost.addTab(tabSpec, indicators[i].getClz(), null);
        }
        //
        if (android.os.Build.VERSION.SDK_INT > 10) {
            mFragmentTabHost.getTabWidget().setShowDividers(0);

            mFragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
                @Override
                public void onTabChanged(String tabId) {
                    if (tabId.equals(getString(Indicator.TEST.getResName()))) {
                        mCount.setVisibility(View.VISIBLE);
                    } else {
                        mCount.setVisibility(View.GONE);
                    }
                }
            });
        }
    }

    public View getRootView() {
        return mRootView;
    }

    public void setTextCount(int count) {
        mCount.setText("?" + count + "");
    }

    /** 
     * @param indicator1
     * @return
     */
    private View getIndicatorView(Indicator indicator1) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
        TextView indicator = (TextView) view.findViewById(R.id.tab_title);

        indicator.setText(getString(indicator1.getResName()));

        indicator.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        Drawable icon = this.getResources().getDrawable(indicator1.getResIcon());
        //        ICON?
        //        icon.setBounds(0, 0, 75, 75);
        //        indicator.setCompoundDrawables(null,icon,null,null);
        indicator.setCompoundDrawablePadding(3);
        indicator.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
        indicator.setPadding(0, 8, 0, 5);

        return view;
    }

    @Override
    public String returnToolBarTitle() {
        return getString(R.string.app_name);
    }

    /**
     * ?--??
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            return mDoubleClickExit.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }
}