2012-02-18 59 views
2

我想我寫在儘可能低的Android版本項目我的請求數,這是11在Android級別<13,結合片段和ActionBar有沒有一種可行的方法?

但我需要Fragment.attach和爲其他動作。

由於11級不包含Fragment.attach,我導入了v4的支持包。

但現在的問題是,ActionLab的TabListerner不使用v4碎片,而是使用11級碎片。鑄造將無法工作。

我真的需要切換到13級還是有實現這一切在11級

這裏一個可行的解決方案是代碼:

import android.app.ActionBar; 
import android.app.Activity; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.app.Fragment; 
import android.app.ActionBar.Tab; 
import android.app.ActionBar.TabListener; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 

public class TestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    } 

    public static class TabListener<T extends android.support.v4.app.Fragment> /* to make sure it take the Fragment from the support package! */ 
    implements ActionBar.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 TabListener(Activity activity, String tag, Class<T> clz) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
    } 

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

    public void onTabSelected(Tab tab, 
     android.support.v4.app.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(android.R.id.content, mFragment, mTag); 
     } else { 
     // If it exists, simply attach it in order to show it 
     ft.attach(mFragment); 
     } 
    } 

    /* these are NOT the implementation of the TabListener above, since the use the 
    * 
    * the FragmentTransactionof the support package and not of level 11 
    * 
    */ 

    public void onTabUnselected(Tab tab, 
     android.support.v4.app.FragmentTransaction ft) { 
     if (mFragment != null) { 
     // Detach the fragment, because another one is being attached 
     ft.detach(mFragment); 
     } 
    } 

    public void onTabReselected(Tab tab, 
     android.support.v4.app.FragmentTransaction ft) { 
     // User selected the already selected tab. Usually do nothing. 
    } 

    /* these are added since they belong to the above definition of TabListener 
    * 
    * unfortunately the use the FragmentTransaction of level 11, not the one of the support package! 
    * 
    */ 
    @Override 
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) { 

    } 

    @Override 
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) { 
    } 

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

    } 

    } 
} 
+0

更新:似乎沒有辦法獲得新的ActionBar和Fragments.attach方法在<13級中正常工作。我最終找到了堅持到級別11的替代方法。 – user387184 2012-02-19 07:30:46

回答

0

不知道你還需要這,但是我們用ActionBarSherlock這聽起來像它可以幫助你還有:使用ActionBarCompat和片段

http://actionbarsherlock.com/

+0

感謝你 - 但我最終決定只使用Android級別13,也因爲我需要的其他功能 – user387184 2012-03-13 15:52:51

1

IM對API的版本8.0,與您的代碼的唯一性差異及其使用ft.replace代替ft.add和ViewPager上的XML展現片段,即時通訊,它運行正常爲止..

import android.app.Activity; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBar.Tab; 

public final class TabListener<T extends Fragment> implements ActionBar.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 TabListener(Activity activity, String tag, Class<T> clz) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
    } 

    /* 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.replace(R.id.pager, mFragment, mTag); 
     } else { 
      // If it exists, simply attach it in order to show it 
      ft.replace(R.id.pager, mFragment, mTag); 
     } 
    } 

    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     if (mFragment != null) { 
      // Detach the fragment, because another one is being attached 
      ft.remove(mFragment); 
     } 
    } 

    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     // User selected the already selected tab. Usually do nothing. 
    } 
} 
相關問題