2012-08-28 36 views
2

我已經閱讀了幾篇關於分離和附加的帖子。但是,因爲我跑API 15即時「重複」的片段與操作欄

應該沒事我從一開始就雙榜一樣,你可以在圖中看到: image of activity

我有以下代碼: (MainActivity)

package dk.metnik.fragmentandactionbar; 

import android.app.ActionBar; 
import android.app.ActionBar.Tab; 
import android.app.ActionBar.TabListener; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import dk.metnik.fragmentandactionbar.fragments.DCIMFragment; 
import dk.metnik.fragmentandactionbar.fragments.ESSFragment; 
import dk.metnik.fragmentandactionbar.fragments.TeamshareFragment; 

public class FragAndBarActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     setupTabs(); 
    } 

    private void setupTabs() { 
     // setup action bar for tabs 
     ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     actionBar.setDisplayShowTitleEnabled(false); 

     Tab tab = actionBar 
       .newTab() 
       .setText("DCIM") 
       .setTabListener(
         new MyTabListener<DCIMFragment>(this, "DCIM", 
           DCIMFragment.class)); 
     actionBar.addTab(tab); 

     tab = actionBar 
       .newTab() 
       .setText("ESS") 
       .setTabListener(
         new MyTabListener<ESSFragment>(this, "ESS", 
           ESSFragment.class)); 
     actionBar.addTab(tab); 

     tab = actionBar 
       .newTab() 
       .setText("TeamShare") 
       .setTabListener(
         new MyTabListener<TeamshareFragment>(this, "TeamShare", 
           TeamshareFragment.class)); 
     actionBar.addTab(tab); 
    } 

    public static class MyTabListener<T extends Fragment> implements 
      TabListener { 
     private Fragment mFragment; 
     private final Activity mActivity; 
     private final String mTag; 
     private final Class<T> mClass; 

     /** 
     * Constructor used each time a new tab is created. 
     * 
     * @param activity 
     *   The host Activity, used to instantiate the fragment 
     * @param tag 
     *   The identifier tag for the fragment 
     * @param clz 
     *   The fragment's Class, used to instantiate the fragment 
     */ 

     public MyTabListener(Activity activity, String tag, Class<T> clz) { 
      mActivity = activity; 
      mTag = tag; 
      mClass = clz; 

      mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag); 
      if (mFragment != null) { // && !mFragment.isDetached()) { 
       FragmentTransaction ft = mActivity.getFragmentManager() 
         .beginTransaction(); 
       // ft.detach(mFragment); 
       ft.remove(mFragment); 
       ft.commit(); 
      } 
     } 

     /* The following are each of the ActionBar.TabListener callbacks */ 

     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      // Check if the fragment is already initialized 
      if (mFragment == null) { 
       // If not, instantiate and add it to the activity 
       mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
       ft.add(R.id.listFragment, mFragment, mTag); 
      } else { 
       ft.attach(mFragment); 
      } 
     } 

     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
      if (mFragment != null) { 
       ft.detach(mFragment); 
      } 
     } 

     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     } 
    } 
} 

而且我的main.xml:

<?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" > 

    <fragment 
     android:id="@+id/listFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?android:attr/actionBarSize"/> 
</LinearLayout> 

回答

1

你通過你方信用證實例與片段的視圖ayout.xml:

<fragment 
    android:id="@+id/listFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="?android:attr/actionBarSize" 
    class="dk.metnik.fragmentandactionbar.fragments.TeamshareFragment" /> 

然後,您正在創建其中第一個標籤將被選擇的標籤系統,增加另一個片段(透明背景我猜)ontop的。

刪除class=""並將您的<fragment>更改爲<FrameLayout>。這將停止初始碎片加載,同時給你一個定義的視圖來容納你的碎片。

此外,您正在將您的新片段添加到Android基礎視圖android.R.id.content。這是合理的,具體取決於你的功能,但如果你希望片段採用元素的空間,你應該使用它的ID。變化:

ft.add(android.R.id.content, mFragment, mTag); 

ft.add(R.id.listFragment, mFragment, mTag); 
+0

試過了。當我刪除班級我得到這個錯誤: http://pastebin.com/sH2wpJp6 –

+0

對不起,沒有看到一層複雜的,調整我的答案。 – Graeme