什么是FragmentTabHost?
在Android较早的API中,使用导航栏一般是采用TabActibvity+TabHost+Activity,但自从Android 3.0中引入了Fragment后,上述方式则不再被官方推荐,而是推荐使用FragmentActivity+FragmentTabHost+Fragement。这样做的原因,想必大家都明白:因为Fragment比Activity更灵活,且更省资源。
因为Fragment是3.0中才引入的,Google为了保持向下兼容的风格,提供了v4包:android.support.v4.app.FragmentTabHost,如下为源码注释,想必大家一看便明白:
* Special TabHost that allows the use of {@link Fragment} objects for
* its tab content. When placing this in a view hierarchy, after inflating
* the hierarchy you must call {@link #setup(Context, FragmentManager, int)}
* to complete the initialization of the tab host.
怎么使用FragmentTabHost?
对于一个知识点,网上肯定有很多文章,大家通过关键词都能搜到其使用方式,但是大家千万别忘了Google也提供了demo,这也是学习的很好方法:support/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.java:
public class FragmentTabs extends FragmentActivity {
private FragmentTabHostmTabHost;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
}
}
对应的布局文件fragment_tabs.xml如下:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> </android.support.v4.app.FragmentTabHost>
其中有两个主要点:android:id/tabhost和@+id/realtabcontent,前者为系统定义的TabHost导航条,后者为主体内容(即Tab点击时,加载Fragment区域),通过使用mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent)将两者结合起来。具体的使用方式,大家可以参照: 使用FragmentTabhost代替TabActibvity 、 Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
其中的注意点
demo运行起来后发现,当从tabA切换到tabB,然后再次切换到tabA时,发现对应的Fragment会重新加载,重新创建布局。这样会引起一个问题:如果布局里有较多的View,则每次tab项切换时重新创建布局会很浪费时间,并且重新创建后,也保存不了之前的状态。对此,网上也有些解决办法:
方法一:在Fragment-onCreateView方法中缓存View
private ViewrootView;//缓存Fragment view
@Override
public ViewonCreateView(LayoutInflaterinflater, ViewGroupcontainer, BundlesavedInstanceState) {
if(rootView==null){
rootView=inflater.inflate(R.layout.tab_fragment, null);
}
ViewGroupparent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
}
return rootView;
}
这种办法会复用之前的View,但是如果在Fragment界面还有数据请求调用的话,比如在onActivityCreated(Bundle savedInstanceState)方法里面还有数据请求的话,它也会重新被调用,如果不希望重新调用,可以采用方法二.
方法二:自定义FragmentTabHost,使用hide()和show()替换detach()和attach()
通过查看FragmentTabHost的源码发现,tab项在切换时,FragmentTransaction是使用detach()来移除fragment的,仔细看该方法:
/**
* Detach the given fragment from the UI. This is the same state as
* when it is put on the back stack: the fragment is removed from
* the UI, however its state is still being actively managed by the
* fragment manager. When going into this state its view hierarchy
* is destroyed.
*
* @param fragment The fragment to be detached.
*
* @return Returns the same FragmentTransaction instance.
*/
public abstract FragmentTransactiondetach(Fragmentfragment);
即可知道为什么会重新创建fragmet了: When going into this state its view hierarchy is destroyed. 。既然知道原理了,我们可以自定义FragmentTabHost,将其中的detach()和attach()替换为hide()和show()。通过动手,我们发现切换tab时,fragment不会再重新创建,达到目的,YES!
总结
这是实现Tab导航的一种方式。当然随着Android Material Design的推出,还有些新的方式实现导航栏: BottomNavigationBar 浅析 ,大家可以学习下。
另外推荐文章: TabHost 和 FragmentTabHost
第一篇Android的文章,如有问题请指出,谢谢!