2012-10-18 62 views
3

我在網上搜索了很多關於在一個操作欄選項卡中有多個片段的可能性。Android - One標籤中的多個片段

This question與我的需求最接近,但代碼無法正常工作。

是否有另一種可能性在一個選項卡中包含多個片段?


這是StartActivity

public class StartActivity extends FragmentActivity implements ActionBar.TabListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_start); 

     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     actionBar.addTab(
       actionBar.newTab() 
      .setText("Karte") 
       .setTabListener(new MapFragmentListener(this))); 

    } 

相應的佈局acticity_start包含用於將放置在所述標籤中的兩個片段的兩個框架佈局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:id="@+id/fragment_map" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <FrameLayout 
     android:id="@+id/fragment_realtime" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

TabListener看起來像這樣:

private class TabListener implements ActionBar.TabListener { 

     private static final String fragment1_tag = "fragment1_tag"; 
     private static final String fragment2_tag = "fragment2_tag"; 

     private FragmentActivity activity; 
     private RealtimeFragment fragment1; 
     private RealtimeFragment fragment2; 

     public TabListener(FragmentActivity activity) { 

      this.activity = activity; 

      android.support.v4.app.FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction(); 

      fragment1 = (RealtimeFragment) activity.getSupportFragmentManager().findFragmentByTag(fragment1_tag); 
      if (fragment1 != null && !fragment1.isDetached()) { 
       ft.detach(fragment1); 
      } 

      fragment2 = (RealtimeFragment) activity.getSupportFragmentManager().findFragmentByTag(fragment2_tag); 
      if (fragment2 != null && !fragment2.isDetached()) { 
       ft.detach(fragment2); 
      } 

      ft.commit(); 
     } 

     @Override 
     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onTabSelected(Tab tab, FragmentTransaction ft) { 

      android.support.v4.app.FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction(); 

      if(fragment1 == null) { 
       fragment1 = new RealtimeFragment(); 
       fragmentTransaction.add(R.id.fragment_map, fragment1, fragment1_tag); 
      } else { 
       fragmentTransaction.attach(fragment1); 
      } 


      if(fragment2 == null) { 
       fragment2 = new RealtimeFragment(); 
       fragmentTransaction.add(R.id.fragment_realtime, fragment2, fragment2_tag); 
      } else { 
       fragmentTransaction.attach(fragment2); 
      } 


      fragmentTransaction.commit(); 

     } 

     @Override 
     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 

      android.support.v4.app.FragmentTransaction fragementTransaction = activity.getSupportFragmentManager().beginTransaction(); 

      if(fragment1_tag != null) 
       fragementTransaction.detach(fragment1); 
      if(fragment2 != null) 
       fragementTransaction.detach(fragment2); 


      fragementTransaction.commit(); 

     } 

    } 

RealtimeFragment和相應的佈局fragment_realtime看起來像這樣:

public class RealtimeFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_realtime, container, false); 
     return view; 
    } 
} 


<?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:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 

在一個選項卡現在兩個類的片段RealtimeFragment應被顯示。片段只顯示一個按鈕,但沒有顯示!爲什麼?

+0

爲什麼不張貼一些代碼,你所得到的錯誤? – Barak

回答

0

我一直在尋找相同主題的解決方案,並且發現這個方法非常方便。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <fragment android:name="com.example.news.ArticleListFragment" 
      android:id="@+id/list" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
    <fragment android:name="com.example.news.ArticleReaderFragment" 
      android:id="@+id/viewer" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
</LinearLayout> 

,從而可以參照您的片段和它充氣,就像一個正常的片段