2012-02-18 10 views
2

내 reqs가 가장 낮은 가능한 Android 버전 (11)으로 프로젝트를 작성하고 싶습니다.안드로이드 수준에서 단편과 ActionBar 결합하기 <13. 가능한 방법이 있습니까?

하지만 Fragment.attach와 ActionBars가 필요합니다.

레벨 11에는 Fragment.attach가 포함되어 있지 않으므로 v4에 대한 지원 패키지를 가져옵니다.

하지만 문제는 ActionTab 용 TabListerner가 v4 Fragment를 사용하지 않고 레벨 11 Fragment를 사용한다는 것입니다. 전송이 작동하지 않습니다. 당신은 여전히이 필요한 경우

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에서 올바르게 작동합니다. 대체 방법을 찾지 못했습니다. – user387184

답변

0

확실하지 :

정말 레벨 13로 전환해야하거나 여기

레벨 11에이 모든 것을 구현하기위한 가능한 솔루션은 코드가입니다 수행 하지만, 우리는 ActionBarSherlock의를 사용하고뿐만 아니라 당신을 도울 수있는 것 같은데 :

http://actionbarsherlock.com/

+0

감사합니다. 그러나 마침내 안드로이드 레벨 13을 사용하기로 결정했습니다. 내가 필요로하는 다른 기능 때문에 – user387184

1

메신저 ActionBarCompat와 조각을 사용하여 API 광고 버전 8.0, 코드 유일한 diference는 조각을 보여주기 위해 XML을 대신 ft.add의 ft.replace과 ViewPager를 사용하여 해당 메신저, 괜찮아요 지금까지 ..

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. 
    } 
} 
관련 문제