MainActivity

Activity

public class MainActivity implements TabHost.OnTabChangeListener {

    private String tabId;
    private FragmentTabHost tabHost;
    private String[] tabText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.flContainer);
        tabHost.getTabWidget().setDividerDrawable(null);
        tabHost.setOnTabChangedListener(this);
        initTab();
        IntentTool.startVersionCheckService(this);
        
    }

    private void initTab() {
        // 获取tab昵称
        tabText = new String[]{getString(R.string.tab_refuel_title),
                getString(R.string.tab_shop_title),
                getString(R.string.tab_server_title),
                getString(R.string.tab_my_title)};
        // 获取切换图片
        int[] imageRes = new int[]{R.drawable.tab_refuel,
                R.drawable.tab_shop,
                R.drawable.tab_server,
                R.drawable.tab_my_center};
        // 获取切换的Fragments
        Class[] fragments = new Class[]{
                RefuelFragment.class,
                ShopFragment.class,
                ServerFragment.class,
                MyFragment.class};

        for (int i = 0; i < tabText.length; i++) {
            View view;
            if (i==0){
                view = View.inflate(this,R.layout.item_tab_s,null);
            }else if(i==1){
                view = View.inflate(this,R.layout.item_tab_s1,null);
            }else{
                view = View.inflate(this,R.layout.item_tab,null);
            }
            ((TextView) view.findViewById(R.id.tv)).setText(tabText[i]);
            ((ImageView) view.findViewById(R.id.iv)).setImageResource(imageRes[i]);
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(tabText[i]).setIndicator(view);
            tabHost.addTab(tabSpec, fragments[i], null);
            tabHost.setTag(i);
        }

        tabHost.getTabWidget().getChildTabViewAt(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabClick(0);
            }
        });
        tabHost.getTabWidget().getChildTabViewAt(1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabClick(1);

            }
        });
        tabHost.getTabWidget().getChildTabViewAt(2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabClick(2);
            }
        });
        tabHost.getTabWidget().getChildTabViewAt(3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabClick(3);
            }
        });


    }

    private void tabClick(int position){
        if (position == 3){
            setStatuBarStyle(false);
            setStatusBarColor(R.color.kRedColor);
        }else{
            setStatuBarStyle(true);
            setStatusBarColor(R.color.colorPrimary);
        }
        String tabStr = tabText[position];
        if (tabStr.equals(tabId)){
//            EventBus.getDefault().post(new TabRefreshEvent(tabId));
        }else{
            tabHost.setCurrentTab(position);
            tabHost.getTabWidget().requestFocus(View.FOCUS_FORWARD);
        }
    }

    @Override
    public void onBackPressed() {
        if (tabText[0].equals(tabId)){
            finish();
        }else{
            tabClick(0);
        }
    }

    @Override
    public void onTabChanged(String s) {
        tabId = s;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onToHomeEvent(ToHomeEvent event){
        String tag = event.getTag();
        if (!TextUtils.isEmpty(tag)){
            tabClick(Integer.parseInt(tag));
        }else{
            tabClick(0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       
    }
}

XML






    
    


    

    
    


你可能感兴趣的:(MainActivity)