Android使用Fragment+FragmentTabHost实现了底部导航栏的效果

1、activity_main.xml




盛京三四线

    


    

        

    


2、tab_view.xml




    

    

3、arrays.xml

 

    
    
        首页
        活动
        分类
        购物车
        我的
    

4、tab_index.xml



    

    

5、divider_line_h.xml




    
    

    

6、IndexFragment.java

public class HomeFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_cart, container, false);
    }
}

7、MainActivity.java

/**
 * 主界面
 */

public class MainActivity extends BaseActivity implements TabHost.OnTabChangeListener {

    private String[] tabTitles;
    private FragmentTabHost tabHost;
    private boolean isExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();   //    控件初始化控件

    }
   //    控件初始化控件
    @Override
    protected void initUI() {
        /** 标题 */
        tabTitles = getResources().getStringArray(R.array.tab_title);
        /** 图标 */
        int[] tabImages = new int[]{R.drawable.tab_index, R.drawable.tab_activity,
                                    R.drawable.tab_class, R.drawable.tab_cart,
                                    R.drawable.tab_member};

        Class[] fragment = new Class[]{IndexFragment.class, ActivityFragment.class,
                                            ClassFragment.class, CartFragment.class,
                                            MemberFragment.class};

        tabHost = getViewById(R.id.tab_host);
        tabHost.setup(this, getSupportFragmentManager(), R.id.real_tab_content);
      /*新建Tabspec选项卡并设置Tab菜单栏的内容和绑定对应的Fragment*/
        for (int i = 0; i < tabTitles.length; i++) {
          // 给每个Tab按钮设置标签、图标和文字
            FragmentTabHost.TabSpec tab = tabHost.newTabSpec(tabTitles[i]).setIndicator(getTabView(tabTitles, tabImages, i));
          // 将Tab按钮添加进Tab选项卡中,并绑定Fragment
            tabHost.addTab(tab, fragment[i], null);
        }

        tabHost.setOnTabChangedListener(this);
        //默认选中是首页
        tabHost.onTabChanged(tabTitles[0]);
    }

    @Override
    protected void bindData() {

    }

    /**
     * 获取tabview
     * @param index
     * @return
     */
    private View getTabView(String[] tabTitles, int[] tabImages ,int index) {
        //将xml布局转换为view对象
        View tabView = LayoutInflater.from(this).inflate(R.layout.tab_view, null);
        //利用view对象,找到布局中的组件,并设置内容,然后返回视图
        TextView tabTextView = getViewById(tabView, R.id.tab_text);
        tabTextView.setText(tabTitles[index]);
        ImageView tabImageView = getViewById(tabView, R.id.tab_image);
        tabImageView.setImageResource(tabImages[index]);
        return tabView;
    }

    @Override
    public void onTabChanged(String tabId) {//Tab改变的时候调用
      //设置Tab被选中的时候颜色改变
        updateTabHost(tabId);
    }

    /**
     * 刷新tabhost 颜色
     * @param tabId
     */
    private void updateTabHost(String tabId) {
        for (int i = 0; i < tabTitles.length; i++) {
            View tabView = tabHost.getTabWidget().getChildTabViewAt(i);
            TextView textView = getViewById(tabView, R.id.tab_text);
            String text = textView.getText().toString();
            if (text.equals(tabId)) {
                textView.setTextColor(Color.parseColor("#8a6432"));
            }
            else {
                textView.setTextColor(Color.parseColor("#333333"));
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (isExit) {
                    finish();
                }
                else {
                    ToastUtil.showShort(this, "再按一次退出");
                    isExit = true;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            isExit = false;
                        }
                    }, 2000);
                }
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

你可能感兴趣的:(Android使用Fragment+FragmentTabHost实现了底部导航栏的效果)