学习笔记 Tianmao 篇 FragmentTabHost (TabHost升级版)

来源:互联网 发布:淘宝怎么加好友 编辑:程序博客网 时间:2024/05/07 01:49

1.首先创建一个Activity继承自FragmentActivity和与之匹配的layout

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!--这里是真正的fragment-->    <FrameLayout        android:id="@+id/realtabcontent"        android:layout_width="fill_parent"        android:layout_height="0dip"        android:layout_weight="1" /><!--这里的id是固定的 详情可以看我前面关于TabHost的文章-->    <android.support.v4.app.FragmentTabHost        android:id="@android:id/tabhost"        android:layout_height="wrap_content"        android:layout_width="match_parent">        <!--这里是假的fragment 不使用他-->        <!--这里的id是固定的 详情可以看我前面关于TabHost的文章-->        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0" />    </android.support.v4.app.FragmentTabHost></LinearLayout>

FragmentActivity里的操作步骤

1.实例化fragmenttabhost

fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

2.这里是与tab同步切换的实际fragment

fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

3.创建以标题为参数的 tabSpec对象

TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(标题);

4.设置tabSpec的视图

        layoutInflater = LayoutInflater.from(this);        View view = layoutInflater.inflate(R.layout.tab_indicator, null);        textView = (TextView) view.findViewById(R.id.txt_indicator);        imageView = (ImageView) view.findViewById(R.id.icon_tab);        textView.setText(tabBean.getTitle());        imageView.setImageResource(tabBean.getIcon());        tabSpec.setIndicator(view);

5.添加实际tabSpec和与之对应的Fragment

fragmentTabHost.addTab(tabSpec, fragment.class, null);

注解

然后我们要自己写与之对应的fragment
这个就不多讲了 后面会贴出代码

代码区

HomeActivity.java pers.xxxxx.tianmao.ui.activity;

public class HomeActivity extends FragmentActivity {    private FragmentTabHost fragmentTabHost;    private LayoutInflater layoutInflater;    private TextView textView;    private ImageView imageView;    private List<TabBean> list = new ArrayList<>(5);    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_loading);        initTab();    }    private void initTab() {        //初始化list        TabBean tab_home = new TabBean(HomeFragment.class, R.drawable.selector_icon_home, R.string.home);        TabBean tab_care = new TabBean(HotFragment.class, R.drawable.selector_icon_care, R.string.care);        TabBean tab_tmzb = new TabBean(CategoryFragment.class, R.drawable.selector_icon_tmzb, R.string.tmzb);        TabBean tab_cart = new TabBean(CartFragment.class, R.drawable.selector_icon_cart, R.string.cart);        TabBean tab_mine = new TabBean(MineFragment.class, R.drawable.selector_icon_mine, R.string.mine);        list.add(tab_home);        list.add(tab_care);        list.add(tab_tmzb);        list.add(tab_cart);        list.add(tab_mine);        //实例化fragmenttabhost        fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);         //            这里是与tab同步切换的实际fragment         fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);        for (TabBean tabBean : list) {            //标题            TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(String.valueOf(tabBean.getTitle()));            //消除分割线            fragmentTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);            layoutInflater = LayoutInflater.from(this);            View view = layoutInflater.inflate(R.layout.tab_indicator, null);            textView = (TextView) view.findViewById(R.id.txt_indicator);            imageView = (ImageView) view.findViewById(R.id.icon_tab);            textView.setText(tabBean.getTitle());            imageView.setImageResource(tabBean.getIcon());            tabSpec.setIndicator(view);            fragmentTabHost.addTab(tabSpec, tabBean.getFragment(), null);        }    }}

TabBean.java pers.lijunxue.tianmao.ui.javabean;

public class TabBean {    private int title;   //每个图片按钮的一个标志    private int icon;    //图片    private Class fragment;  //必须加载的fragment,类似于HomeFragment        public TabBean(Class fragment, int icon, int title)        {        this.fragment = fragment;            this.icon = icon;            this.title = title;        }        public int getIcon()        {            return icon;        }        public void setIcon(int icon)        {            this.icon = icon;        }        public int getTitle()        {            return title;        }        public void setTitle(int title)        {            this.title = title;        }        public Class getFragment()        {            return fragment;        }        public void setFragment(Class fragment)        {            this.fragment = fragment;        }}

selector_icon_care.xml 举一个例子 其他几个都一样的

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- Non focused states -->    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/icon_care_n" />    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/icon_care_press" />    <!-- Focused states -->    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/icon_care_press" />    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/icon_care_press" />    <!-- Pressed -->    <item android:state_selected="true" android:state_pressed="true" android:drawable="@mipmap/icon_care_press" />    <item android:state_pressed="true" android:drawable="@mipmap/icon_care_press" /></selector>

大家还记得上面有这样一句代码吧layoutInflater.inflate(R.layout.tab_indicator, null);

tab_indicator.xml

<!--这个布局是fragmentTabHost中一个按钮的布局--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_gravity="center"    android:paddingTop="3dp"    android:paddingBottom="3dp"    android:gravity="center">    <ImageView        android:id="@+id/icon_tab"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        />    <TextView        android:id="@+id/txt_indicator"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="2dp"        android:textColor="@color/selector_tab_text"        /></LinearLayout>

fragment 空白的类 同学们可以自己添加view

public class HomeFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_home, container, false);        return view;    }}
0 0
原创粉丝点击