2015-04-16 42 views
0

我想在片段內部創建選項卡。我設法在片段內創建制表符,但現在我想爲每個製表符加載不同的片段(fragment1.class和fragment2.class)。如何在選項卡中加載片段視圖

任何人都可以請建議如何加載每個片段到他們各自的標籤?

下面是我的主要片段,是持有選項卡。

非常感謝!

public class BusFragment extends Fragment { 

    private TabHost mTabHost; 
    View rootView; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      rootView = inflater.inflate(R.layout.fragment_bus, container, false); 
      mTabHost = (TabHost) rootView.findViewById(R.id.tabHost); 
      mTabHost.setup(); 

      TabHost.TabSpec spec = mTabHost.newTabSpec("tag"); 
      spec.setIndicator("Fragment1"); 
      spec.setContent(new TabHost.TabContentFactory() { 

       @Override 
       public View createTabContent(String tag) { 
        // TODO Auto-generated method stub 
        return (new AnalogClock(getActivity())); 

       } 
      }); 
      mTabHost.addTab(spec); 

      spec = mTabHost.newTabSpec("tag1"); 
      spec.setIndicator("Fragment2"); 
      spec.setContent(new TabHost.TabContentFactory() { 

       @Override 
       public View createTabContent(String tag) { 
        // TODO Auto-generated method stub 
        return (new AnalogClock(getActivity())); 
       } 
      }); 
      mTabHost.addTab(spec); 

      spec = mTabHost.newTabSpec("tag2"); 
      spec.setIndicator("Fragment3"); 
      spec.setContent(new TabHost.TabContentFactory() { 

       @Override 
       public View createTabContent(String tag) { 
        // TODO Auto-generated method stub 
        return (new AnalogClock(getActivity())); 
       } 
      }); 
      mTabHost.addTab(spec); 

      return rootView; 
     } 
} 

回答

0

你好如果你想加載片段裏面的片段android提供了Child Fragment Manager而不是普通的片段管理器。當我點擊選項卡想要加載不同的片段時,我遇到了同樣的問題,我在該選項卡內部有片段。看到下面的步驟希望能幫助你。

第1步。請參閱我的StatisticsFragment與它的佈局fragment_statistics。

public class StatisticsFragment extends Fragment { 

    private FragmentTabHost mTabHost; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_statistics,container, false); 
     mTabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost); 

     mTabHost.setup(getActivity(), getChildFragmentManager(),R.id.tabFrameLayout); 
     mTabHost.addTab(mTabHost.newTabSpec("WEEK").setIndicator(buildTabLayout(EnumStatistics.WEEKLY)),WeekFragment_.class, null); 
     mTabHost.addTab(mTabHost.newTabSpec("ALL").setIndicator(buildTabLayout(EnumStatistics.ALL)),DetailedFragment_.class, null); 

     mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new WeekTabClick()); 

     mTabHost.getTabWidget().getChildAt(1).setOnClickListener(new AllTabClick()); 

     return rootView; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    } 

    private View buildTabLayout(EnumStatistics enumStatistics) { 
     View tab; 
     if (enumStatistics == EnumStatistics.WEEKLY) { 
      tab = getActivity().getLayoutInflater().inflate(R.layout.tab_week_layout, null); 
     } else if (enumStatistics == EnumStatistics.MONTHLY) { 
      tab = getActivity().getLayoutInflater().inflate(R.layout.tab_month_layout, null); 
     } else if (enumStatistics == EnumStatistics.YEAR) { 
      tab = getActivity().getLayoutInflater().inflate(R.layout.tab_year_layout, null); 
     } else { 
      tab = getActivity().getLayoutInflater().inflate(R.layout.tab_detailed_layout, null); 
     } 
     return tab; 
    } 



    public class WeekTabClick implements View.OnClickListener { 

     @Override 
     public void onClick(View v) { 

      mTabHost.getTabWidget().focusCurrentTab(0); 
      FragmentTransaction ft = getChildFragmentManager().beginTransaction(); 
      ft.replace(R.id.tabFrameLayout, WeekFragment_.instantiate(getActivity(), WeekFragment_.class.getName())); 
      ft.addToBackStack(null); 
      ft.commit(); 

     } 
    } 

    public class AllTabClick implements View.OnClickListener { 

     @Override 
     public void onClick(View v) { 

      mTabHost.getTabWidget().focusCurrentTab(1); 
      FragmentTransaction ft = getChildFragmentManager() 
        .beginTransaction(); 
      ft.replace(R.id.tabFrameLayout, DetailedFragment_.instantiate(
        getActivity(), DetailedFragment_.class.getName())); 
      ft.addToBackStack(null); 
      ft.commit(); 

     } 
    } 
} 

在上面buildTabLayout功能意圖建立標籤控件的自定義佈局,你可以把你想要的標籤控件中顯示簡單的文本或圖像。

步驟2. fragment_statistics.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:relaxis="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <android.support.v4.app.FragmentTabHost 
     android:id="@+id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/croutonview" 
     android:gravity="center" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <TabWidget 
       android:id="@+id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="48dp" 
       android:orientation="horizontal" 
       /> 

      <FrameLayout 
       android:id="@+id/tabFrameLayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </android.support.v4.app.FragmentTabHost> 

</RelativeLayout> 

正如你在裏面的佈置上面看到tabFrameLayout我們加載了所有子片段Programmaticly。

讓我知道你是否有任何疑問和疑問。謝謝

相關問題