Android两级导航菜单栏的实现--FragmentTabHost嵌套FragmentTabHost

 开发APP,有时候一层Tab导航菜单栏并不能满足业务需求,这时候就需要二级Tab导航菜单栏了。接下来的两篇博客实现的都是这种效果,只是采用的方式不同而已。

        本篇实现的类似Instagram(我天朝一直喜欢各种封杀)的效果:

 

        这是本篇中Demo实现的效果:

 

 

        思路:采取FragmentTabHost嵌套FragmentTabHost实现,关于FragmentTabHost和Fragment的常见方法和使用,在Android Tab导航菜单栏--FragmentTabHost+Fragment实现(基础篇)已经讲得很清楚了。本篇代码是在前面博客代码基础上实现的。
因为前面的博文已经讲得很清楚了,所以本篇不再作详细说明了。
 好了,上代码吧:

重写 fragment_message1.xml

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical" >  
  11.   
  12.         <TabWidget  
  13.             android:id="@android:id/tabs"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content" >  
  16.         </TabWidget>  
  17.         <!-- tabhost上面一条黑色分割 @drawable/line_shop -->  
  18.   
  19.         <View  
  20.             android:id="@+id/view_2"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="0.1dip"  
  23.             android:background="#D1D1D1" >  
  24.         </View>  
  25.         <!-- android:layout_height="0dp"必须设置成0 ,否则底部tab选项卡会被遮盖住 -->  
  26.         <!-- 这里是tab选项卡的内容 ,宽度要填满,高度自动适应 -->  
  27.   
  28.         <FrameLayout  
  29.             android:id="@android:id/tabcontent"  
  30.             android:layout_width="fill_parent"  
  31.             android:layout_height="0dip"  
  32.             android:layout_weight="1" >  
  33.         </FrameLayout>  
  34.         <!-- 调换framelayout和tabwidget的前后顺序可以分别实现tab的top和在底下的效果 -->  
  35.   
  36.     </LinearLayout>  
  37.   
  38. </android.support.v4.app.FragmentTabHost>  


        注意XML中的布局,TabWidget一定放在FrameLayout的上面。
        本篇的子FragmentTabHost使用的是android.support.v4.app.FragmentTabHost,所以子Fragment会执行OncreateView(),如果不想重复执行OncreateView()或者想要保存子Fragment的状态,看看前面的博客,自己稍微改一下代码就OK了,这里不再重复叙述。

 

重写 FragmentMessage

 

[java] view plain copy
 
  1. public class FragmentMessage extends Fragment {  
  2.   
  3.     FragmentTabHost mTabHost = null;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.   
  8.         super.onCreate(savedInstanceState);  
  9.           
  10.         Bundle bundle = getArguments();  
  11.         if (null != bundle) {  
  12.             //  
  13.         }  
  14.     }  
  15.   
  16.     @Override  
  17.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  18.             Bundle savedInstanceState) {  
  19.         T.showShort(getActivity(), "FragmentMessage==onCreateView");  
  20.         View view = inflater.inflate(R.layout.fragment_message1, null);  
  21.   
  22.         initView(view);  
  23.   
  24.         return view;  
  25.     }  
  26.   
  27.     private void initView(View view) {  
  28.   
  29.         mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);  
  30.           
  31.         mTabHost.setup(getActivity(), getChildFragmentManager(),  
  32.                 android.R.id.tabcontent);  
  33.   
  34.         mTabHost.addTab(mTabHost.newTabSpec("sub1").setIndicator("新闻"),  
  35.                 SubFragment1.class, null);  
  36.   
  37.         mTabHost.addTab(mTabHost.newTabSpec("sub2").setIndicator("财经"),  
  38.                 SubFragment2.class, null);  
  39.   
  40.         mTabHost.addTab(mTabHost.newTabSpec("sub3").setIndicator("旅游"),  
  41.                 SubFragment3.class, null);  
  42.           
  43.         mTabHost.getTabWidget().setDividerDrawable(R.color.white);  
  44.   
  45.     }  
  46.   
  47. }<span style="background-color: rgb(254, 254, 254); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; font-size: 16px; line-height: 27.1875px;">   </span>  

      这里的setIndicator,我们只使用文字标识tab

 

再添加 SubFragment1(这里只给出一个,其它类似)

 

 

[java] view plain copy
 
  1. public class SubFragment1 extends Fragment {  
  2.   
  3.     @Override  
  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  5.             Bundle savedInstanceState) {  
  6.         T.showShort(getActivity(), "SubFragment1==onCreateView");  
  7.         TextView tv = new TextView(getActivity());  
  8.         tv.setTextSize(25);  
  9.         tv.setBackgroundColor(Color.parseColor("#FFA07A"));  
  10.         tv.setText("新闻");  
  11.         tv.setGravity(Gravity.CENTER);  
  12.         return tv;  
  13.     }  
  14. }  

 

 

Demo1下载地址:http://download.csdn.net/detail/yalinfendou/8543981

posted @ 2016-11-30 21:09  天涯海角路  阅读(559)  评论(0)    收藏  举报