目前主流的app主頁都是由幾個tab頁組成,因此我們開發app的時候一般都會涉及到主頁tab的切換實現。常用的主頁tab切換實現有viewpage和FragmentActivity組合,FragmentTransaction的add、replace、remove、hide和show方法,以及Android官方的FragmentTabHost。看標題,這次我們只講FragmentTabHost的tab實現。
首先是3個效果圖



三星note4 小米2s 小米2s
上面是使用FragmentTabHost實現的3個效果圖。
下面來一段一般FragmentTabHost的tab實現
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#89c"    android:fitsSystemWindows="false"    android:gravity="center_horizontal"    android:orientation="vertical">    <FrameLayout        android:id="@+id/tabContent"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" />    <android.support.v4.app.FragmentTabHost        android:id="@android:id/tabhost"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0" />    </android.support.v4.app.FragmentTabHost></LinearLayout>Activity代碼如下:package hai.com.android_test.ui;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;import android.util.Log;import android.widget.FrameLayout;import android.widget.TabHost;import butterknife.BindView;import butterknife.ButterKnife;import hai.com.android_test.R;import hai.com.android_test.ui.fragment.MyFragment;public class HostTabActivity extends FragmentActivity {    PRivate static final String TAG = "HostTabActivity";    @BindView(R.id.tabContent)    FrameLayout tabContent;    @BindView(android.R.id.tabhost)    FragmentTabHost tabhost;    private String tabSpecs[] = {"首頁", "消息", "好友", "廣場"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_tabhost);        ButterKnife.bind(this);        init();    }    private void init() {        tabhost.setup(this, getSupportFragmentManager(), R.id.tabContent);        for (int i = 0; i < tabSpecs.length; i++) {            TabHost.TabSpec tabSpec = tabhost.newTabSpec(tabSpecs[i]).setIndicator(tabSpecs[i]);            Bundle b = new Bundle();            b.putString("data", tabSpecs[i]);            tabhost.addTab(tabSpec, MyFragment.class, b);        }        tabhost.setBackgroundColor(Color.WHITE);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");        super.onActivityResult(requestCode, resultCode, data);    }}由于MyFragment的代碼過于簡單,就不貼了。
附上一段日志
02-24 11:22:58.587 9886-9886/hai.com.android_test E/HostTabActivity: onStart: 02-24 11:22:58.587 9886-9886/hai.com.android_test E/HostTabActivity: onResume: 02-24 11:22:58.587 9886-9886/hai.com.android_test E/HostTabActivity: onResumeFragments: 02-24 11:22:58.597 9886-9886/hai.com.android_test E/MyFragment: MyFragment: null02-24 11:22:58.597 9886-9886/hai.com.android_test E/MyFragment: onAttach: null02-24 11:22:58.597 9886-9886/hai.com.android_test E/MyFragment: onCreate: 首頁02-24 11:22:58.597 9886-9886/hai.com.android_test E/MyFragment: onCreateView:  首頁02-24 11:22:58.597 9886-9886/hai.com.android_test E/MyFragment: onViewCreated:  首頁02-24 11:22:58.597 9886-9886/hai.com.android_test E/MyFragment: onActivityCreated: 首頁點擊消息Tab后02-24 11:23:14.857 9886-9886/hai.com.android_test E/MyFragment: MyFragment: null02-24 11:23:14.862 9886-9886/hai.com.android_test E/MyFragment: onAttach: null02-24 11:23:14.862 9886-9886/hai.com.android_test E/MyFragment: onCreate: 消息02-24 11:23:14.862 9886-9886/hai.com.android_test E/MyFragment: onDestroyView:  首頁02-24 11:23:14.862 9886-9886/hai.com.android_test E/MyFragment: onCreateView:  消息02-24 11:23:14.867 9886-9886/hai.com.android_test E/MyFragment: onViewCreated:  消息02-24 11:23:14.867 9886-9886/hai.com.android_test E/MyFragment: onActivityCreated: 消息02-24 12:38:06.837 9886-9886/hai.com.android_test E/MyFragment: onDestroyView:  消息02-24 12:38:06.837 9886-9886/hai.com.android_test E/MyFragment: onCreateView:  首頁02-24 12:38:06.842 9886-9886/hai.com.android_test E/MyFragment: onViewCreated:  首頁02-24 12:38:06.842 9886-9886/hai.com.android_test E/MyFragment: onActivityCreated: 首頁說明:通過日志發現當各個tab切換的時候:每個tab都是延遲加載的,點擊一個tab的時候,前一個tab就會執行onDestroyView,進入的這個就會執行onAttach、onCreate...onActivityCreated。即每個tab的Fragment都是onCreate、onDestroy一次,onCreateView和onDestroyView執行多次。從日志觀察我們可以實現一個需求:Fragment在FragmentTabHost中可以實現延遲加載的需求。
下面說下上面為什么會有3張效果圖

觀察發現FragmentTabHost實現的tab在不同的手機上會有不同的效果,在三星note4(6.0)上各個tab之間沒有分割線,而在小米2s(5.0)上卻有分割線,這是我們需要注意的地方。
其實通過 TabSpec.setIndicator(View view) 方法我們也可以自定義tabView,以實現自定義效果,如第三個的效果圖的tabView就是自定義實現。
新聞熱點
疑難解答