美文网首页
Android使用Fragment+FragmentTabHos

Android使用Fragment+FragmentTabHos

作者: Courage_SC | 来源:发表于2017-10-20 16:11 被阅读71次

    1、activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="net.sytm.bscshop.activity.main.MainActivity"
        android:orientation="vertical"
        android:divider="@drawable/divider_line_h"
        android:showDividers="middle">
    
    

    盛京三四线

        <FrameLayout
            android:id="@+id/real_tab_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
    
        <android.support.v4.app.FragmentTabHost
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tab_host">
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />
    
        </android.support.v4.app.FragmentTabHost>
    </LinearLayout>
    
    

    2、tab_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:src="@drawable/tab_index"
            android:id="@+id/tab_image"
            android:layout_marginTop="4dp"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="首页"
            android:id="@+id/tab_text"
            android:layout_marginBottom="4dp"/>
    </LinearLayout>
    

    3、arrays.xml

     <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!--底部菜单-->
        <string-array name="tab_title">
            <item>首页</item>
            <item>活动</item>
            <item>分类</item>
            <item>购物车</item>
            <item>我的</item>
        </string-array>
    </resources>
    

    4、tab_index.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/footbg1s"
            android:state_selected="true" />
    
        <item android:drawable="@drawable/footbg1" />
    </selector>
    

    5、divider_line_h.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
        <!--水平分割线用line 高度大于宽度-->
        <stroke android:color="#dfdfdf" android:width="1dp" />
    
        <size android:height="2dp" />
    </shape>
    

    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+FragmentTabHos

          本文链接:https://www.haomeiwen.com/subject/kcevuxtx.html