Android Fragment Base

分享到:
public class FragmentTabsActivity extends FragmentActivity implements OnClickListener {

    //定义FragmentTabHost对象
    private SaveFragmentTabHost mTabHost;
    //定义数组来存放Fragment界面
    private final Class[] fragmentArray = { A.class, B.class, C.class, D.class, E.class,
            F.class, G.class, H.class, I.class };
    //定义数组来存放按钮图片
    private int mIconFontArray[] = { R.string.A, R.string.B, R.string.C, R.string.D, R.string.E };
    private int mIconSelectArray[] = { R.string.AA, R.string.BB, R.string.CC, R.string.DD,
            R.string.EE};
    //Tab选项卡的文字
    private long keyNum;

    private LinearLayout tabsParent;
    private MenuDialog menuDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        keyNum = 0;
    }

    /**
     * 在这里获取到每个需要用到的控件的实例,并给它们设置好必要的点击事件。
     */
    private void initViews() {
        //实例化布局对象
        // 得到fragment的个数
        int count = fragmentArray.length;
        tabsParent = (LinearLayout) findViewById(R.id.tabsParent);
        //实例化TabHost对象,得到TabHost
        mTabHost = (SaveFragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        for (int i = 0; i < count; i++) {
            // 为每一个Tab按钮设置图标、文字和内容
            String name = String.valueOf(i);
            TabSpec tabSpec = mTabHost.newTabSpec(name).setIndicator(name);
            //将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            //设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab);
        }

mytabs = getTabHost();
mytabs.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
}
});

        setupTabsBar();
        menuDialog = new MenuDialog(mActivity, mTabHost);
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private LinearLayout homeLayout, celebrityLayout, brandLayout, exploreLayout, moreLayout;
    private TextView homeTv, celebrityTv, brandTv, exploreTv, moreTv;

    private void setupTabsBar() {
        homeLayout = (LinearLayout) findViewById(R.id.tabLayoutHome);
        homeLayout.setSelected(true);
        celebrityLayout = (LinearLayout) findViewById(R.id.tabLayoutCelebrity);
        brandLayout = (LinearLayout) findViewById(R.id.tabLayoutBrand);
        exploreLayout = (LinearLayout) findViewById(R.id.tabLayoutExplore);
        moreLayout = (LinearLayout) findViewById(R.id.tabLayoutMore);

        homeLayout.setOnClickListener(this);
        celebrityLayout.setOnClickListener(this);
        brandLayout.setOnClickListener(this);
        exploreLayout.setOnClickListener(this);
        moreLayout.setOnClickListener(this);

        homeTv = (TextView) findViewById(R.id.tabIconHome);
        celebrityTv = (TextView) findViewById(R.id.tabIconCelebrity);
        brandTv = (TextView) findViewById(R.id.tabIconBrand);
        exploreTv = (TextView) findViewById(R.id.tabIconExplore);
        moreTv = (TextView) findViewById(R.id.tabIconMore);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.tabLayoutHome:
            setCurrentFragment(0);
            break;
        case R.id.tabLayoutCelebrity:
            setCurrentFragment(1);
            break;
        case R.id.tabLayoutBrand:
            setCurrentFragment(2);
            break;
        case R.id.tabLayoutExplore:
            setCurrentFragment(3);
            break;
        case R.id.tabLayoutMore:
            setCurrentFragment(4);
            menuDialog.home();
            break;
        }
    }

    private void setCurrentFragment(int index) {
        homeLayout.setSelected(index == 0);
        celebrityLayout.setSelected(index == 1);
        brandLayout.setSelected(index == 2);
        exploreLayout.setSelected(index == 3);
        moreLayout.setSelected(index == 4);

        homeTv.setText(index == 0 ? getResources().getString(mIconSelectArray[0]) : getResources().getString(mIconFontArray[0]));
        celebrityTv.setText(index == 1 ? getResources().getString(mIconSelectArray[1]) : getResources().getString(mIconFontArray[1]));
        brandTv.setText(index == 2 ? getResources().getString(mIconSelectArray[2]) : getResources().getString(mIconFontArray[2]));
        exploreTv.setText(index == 3 ? getResources().getString(mIconSelectArray[3]) : getResources().getString(mIconFontArray[3]));
        moreTv.setText(index == 4 ? getResources().getString(mIconSelectArray[4]) : getResources().getString(mIconFontArray[4]));
        if (index != 4)
            mTabHost.setCurrentTab(index);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - keyNum) > 2000) {
                Toast.makeText(mActivity, "再按一次退出", Toast.LENGTH_SHORT).show();
                keyNum = System.currentTimeMillis();
            } else {
                imageLoader.stop();
                System.exit(0);
                finish();
                android.os.Process.killProcess(android.os.Process.myPid());
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
昵    称:
验证码:

相关文档: