tarotaroのエンジニア生活

技術ネタとか日々の仕事の話とか

FragmentでTabHostを使いたい

ActivityでTabHostを使う場合、TabActivityを継承して画面を
構成するが、Fragmentには、TabFragmentなんてものはないので、
どうしたもんかとつまると思う。
まず、Layoutはつぎのように構成します。このlayoutをtab.xmlとします。

<?xml version="1.0" encoding="utf-8"?> 
 
    android:id="@android:id/tabhost"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    > 
     
    <LinearLayout  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:background="#FFFFFF"  
        >  
        <include layout="@layout/actionbar_btn" />
        <TabWidget  
            android:id="@android:id/tabs"  
            android:layout_width="fill_parent"  
            android:layout_height="45.333dp" 
            android:gravity="center" 
            />  
         <FrameLayout  
            android:id="@android:id/tabcontent"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content" >
        <FrameLayout
                android:id="@+id/tab_view1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

        <FrameLayout
                android:id="@+id/tab_view2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </FrameLayout>
    </LinearLayout>  
</TabHost>  



つぎにいままで、TabActivityだったもののFragment化したものソースの一部

public class TabFragment extends Fragment  {
    /** Called when the activity is first created. */
    private final static String TAB1 = "Tab1";
    private final static String TAB2 = "Tab2";
    

    private View mSelfView;
    private TabHost mTabHost;
  

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab, container, false);
        mSelfView=view;

        LayoutInflater inflate = LayoutInflater.from(getActivity());


        TabHost tabs = (TabHost)view.findViewById(android.R.id.tabhost);
        tabs.setup();
        mTabHost =tabs;
        TabSpec spec;
        

        View tabView1 = inflate.inflate(R.layout.tabview, null);

        
        spec = tabs.newTabSpec(TAB1);
        spec.setIndicator(tabView1);
        spec.setContent(R.id.tab_view1);
        tabs.addTab(spec);

        View tabView2 = inflate.inflate(R.layout.tabview, null);


        spec = tabs.newTabSpec(TAB2);
        spec.setIndicator(tabView2);
        spec.setContent(R.id.tab_view2);
        tabs.addTab(spec);
        
            onTabChanged(TAB1);
        

        tabs.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                TabFragment.this.onTabChanged(s);
            }
        });
       
        return view;
    }

    public void onTabChanged(String tabId) {
        TabHost tabhost=(TabHost)mSelfView.findViewById(android.R.id.tabhost);
        int id = tabhost.getCurrentTab();
       
      
        FragmentManager fm = getFragmentManager();
        if (fm.findFragmentByTag(tabId) == null) {
            if(tabId.equals(TAB1)){
                Fragment fragment = new Tab1Fragment();
                        fm.beginTransaction()
                        .replace(R.id.tab_view1, fragment, tabId)
                        .commit();
            }
            else{
                Fragment fragment = new Tab2Fragment();
                fm.beginTransaction()
                        .replace(R.id.tab_view2, fragment, tabId)
                        .commit();
            }
        }
    }
 
        その他のコードを書く
}
 
tabviewはタブの部分のビューのlayoutで、Tab1Fragmentおよび
Tab2Fragmentは、TabActivityを使ったときのそれぞれのタブが切り替わったときのActivityの
代わりにFragmentを使う。ちなみに、Tab1Fragmentとかは、初期化のタイミングとかで
UIの初期化にちょっと癖があったりするので注意する。


こんな感じ。