簡體   English   中英

來自Android 2.3.3帶有片段的布局XML的ActionBar

[英]ActionBar, from Android 2.3.3 Layout XML with Fragments

嗨,我需要在我的應用程序中實現ActionBar,但我不知道從哪里開始。

我希望得到這個例子
鏈接

我已經測試了ShareLock和其他東西,但是我無法成功實現這些示例,我需要使用Fragments獲得此結果,並且應該有4個選項卡。

我用過android-support-v4.jar

這是我使用的片段實用程序。

public class TabManager implements TabHost.OnTabChangeListener {
    private final FragmentActivity mActivity;
    private final TabHost mTabHost;
    private final int mContainerId;
    private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
    TabInfo mLastTab;

    static final class TabInfo {
    private final String tag;
    private final Class<?> clss;
    private final Bundle args;
    private Fragment fragment;

    TabInfo(String _tag, Class<?> _class, Bundle _args) {
        tag = _tag;
        clss = _class;
        args = _args;
    }
}

static class DummyTabFactory implements TabHost.TabContentFactory {
    private final Context mContext;

    public DummyTabFactory(Context context) {
        mContext = context;
    }

    @Override
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }
}

public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
    mActivity = activity;
    mTabHost = tabHost;
    mContainerId = containerId;
    mTabHost.setOnTabChangedListener(this);
}

public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
    tabSpec.setContent(new DummyTabFactory(mActivity));
    String tag = tabSpec.getTag();

    TabInfo info = new TabInfo(tag, clss, args);

    // Check to see if we already have a fragment for this tab, probably
    // from a previously saved state.  If so, deactivate it, because our
    // initial state is that a tab isn't shown.
    info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
    if (info.fragment != null && !info.fragment.isDetached()) {
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
        ft.detach(info.fragment);
        ft.commit();
    }

    mTabs.put(tag, info);
    mTabHost.addTab(tabSpec);
}

@Override
public void onTabChanged(String tabId) {

    TabInfo newTab = mTabs.get(tabId);
    FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
    if (mLastTab != newTab) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mActivity,
                        newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }
        mLastTab = newTab;
        ft.commit();
        mActivity.getSupportFragmentManager().executePendingTransactions();
    }
}

}

還有更多...

public class FragmentsUtils {
private static String DIALOG_TAG = "dialog_tag";

/*public static MyAlertDialogFragment showDialog(FragmentManager fragmentManager, int id) {
    // DialogFragment.show() will take care of adding the fragment
    // in a transaction.  We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = fragmentManager.beginTransaction();
    Fragment prev = fragmentManager.findFragmentByTag(DIALOG_TAG);
    if (prev != null) {
        ft.remove(prev);
    }

    //      ft.addToBackStack(null);

    // Create and show the dialog.
    MyAlertDialogFragment newFragment = MyAlertDialogFragment.newInstance(id);
    newFragment.show(ft, DIALOG_TAG);
    return newFragment;
}*/

public static void swapFragments(FragmentManager fragmentManager, int containerViewId, Fragment newFragment, String fragmentTag) {

    if (fragmentManager == null)
        return;
    View newFragmentMainView = newFragment.getView();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    //      fragmentTransaction.replace(containerViewId, fragment);

    if (fragmentTag == null || fragmentTag.length() < 1)
        fragmentTransaction.add(containerViewId, newFragment);
    else
        fragmentTransaction.add(containerViewId, newFragment, fragmentTag);

    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    fragmentTransaction.commit();
}

public static void swapFragments(FragmentManager fragmentManager, int containerViewId, Fragment newFragment) {
    swapFragments(fragmentManager, containerViewId, newFragment, null);
}

}

提前謝謝了...

僅從API 11(3)起可以使用ActionBar 如果您嘗試在API 10(2.3.3-2.3.7)上的設備上運行使用ActionBar的應用程序,則該應用程序將不可見。

如果您在應用程序中需要ActionBar (本質上是向Tabs提供的強大產品)並計划針對API 10或更早版本的設備,則可以查看android-actionbarActionBarSherlock

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM