16

我使用 Android 已经有一段时间了,但 Fragment 对我来说有点新(可能对大多数人来说都是如此)。无论如何,我有下面的代码,它工作正常。我有三个片段,每个选项卡中都有一个。我想知道每次切换选项卡时调用 onCreateView 是否正常,这样做有意义吗?每次选项卡更改时不应该有办法不重绘片段吗?

我正在从一个有 3 个活动的应用程序转换它,每个选项卡中有一个活动,每次选项卡更改时重新创建视图似乎是一种浪费,而以前在选项卡更改之间存在视图就很好了。 .

顺便说一句,此代码借自:http ://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

public class Tabs extends FragmentActivity implements
    TabHost.OnTabChangeListener {

final String MAP_TAB = "Map";
final String IMAGES_TAB = "Images";
final String SETTINGS_TAB = "Settings";

TabHost mTabHost;
HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabInfo>();
TabInfo mLastTab = null;

private class TabInfo {
    private String tag;
    private Class clss;
    private Bundle args;
    private Fragment fragment;
    TabInfo(String tag, Class clazz, Bundle args) {
        this.tag = tag;
        this.clss = clazz;
        this.args = args;
    }

}

class TabFactory implements TabContentFactory {

    private final Context mContext;

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

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

}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    initialiseTabHost(savedInstanceState);
    if (savedInstanceState != null)
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}

protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
    super.onSaveInstanceState(outState);
}

private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo;

    Tabs.addTab(this,
            mTabHost,
            mTabHost.newTabSpec(MAP_TAB).setIndicator(
                    MAP_TAB,
                    getResources().getDrawable(R.drawable.ic_tab_map_states)),
            ( tabInfo = new TabInfo(MAP_TAB, HMapFragment_NEW.class, args)));
    mapTabInfo.put(tabInfo.tag, tabInfo);

    Tabs.addTab(this,
            mTabHost,
            mTabHost.newTabSpec(IMAGES_TAB).setIndicator(
                    IMAGES_TAB,
                    getResources().getDrawable(R.drawable.ic_tab_gallery_states)),
            ( tabInfo = new TabInfo(IMAGES_TAB, ImageGridFragment.class, args)));
    mapTabInfo.put(tabInfo.tag, tabInfo);

    Tabs.addTab(this,
            mTabHost,
            mTabHost.newTabSpec(SETTINGS_TAB).setIndicator(
                    SETTINGS_TAB,
                    getResources().getDrawable(R.drawable.ic_tab_settings_states)),
            ( tabInfo = new TabInfo(SETTINGS_TAB, SettingsFragment.class, args)));
    mapTabInfo.put(tabInfo.tag, tabInfo);

    // Default to first tab
    this.onTabChanged(MAP_TAB);
    mTabHost.setOnTabChangedListener(this);
}

private static void addTab(Tabs activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    // Attach a Tab view factory to the spec
    tabSpec.setContent(activity.new TabFactory(activity));
    String tag = tabSpec.getTag();

    // 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.
    tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
    if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        ft.detach(tabInfo.fragment);
        ft.commit();
        activity.getSupportFragmentManager().executePendingTransactions();
    }

    tabHost.addTab(tabSpec);
}

public void onTabChanged(String tag) {
    TabInfo newTab = this.mapTabInfo.get(tag);
    // if they've clicked to change tabs
    if (mLastTab != newTab) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        if (mLastTab != null)
            if (mLastTab.fragment != null) ft.detach(mLastTab.fragment);
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(this, newTab.clss.getName(), newTab.args);
                ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
            } else ft.attach(newTab.fragment);
        }

        mLastTab = newTab;
        ft.commit();
        this.getSupportFragmentManager().executePendingTransactions();
    }
}
}
4

1 回答 1

24

有 3 种方法可以从视图中删除片段:

  • 隐藏它(hide交易对象上的功能)
  • 分离它(detach事务对象上的函数)
  • 删除它(remove交易对象上的功能)

如果隐藏它,视图将被隐藏,但仍位于布局中并且应该保持不变。如果分离它,视图将被拆除,但片段仍由 FragmentManager 管理(例如,将在配置更改时重新创建)。如果您删除它,它将完全从 FragmentManager 中删除,并且它的状态将不再被管理。

于 2012-06-12T19:54:25.947 回答