即使片段被隐藏,FragmentTabHost也会触发onCreateView

时间:2013-07-25 14:27:42

标签: android android-fragments android-tabhost fragmenttransaction fragment-tab-host

我在片段中有一个FragmentTabHost。我有4个标签。当我单击选项卡时,即使我使用片段事务来隐藏它,也会触发选项卡的onCreateView?

我调试并发现当我从主页选项卡中单击“创建”选项卡时

CreateFragment createFragment = (CreateFragment) fm.findFragmentByTag(CREATE_TAG);
即使我在tabHost.addTab中添加了

仍然为空?

如何在标签之间移动而不是调用每个片段的onCreateView?

谢谢

这是片段类

public class TabFragment extends Fragment {

public static final String HOME_TAG = "home";
public static final String CREATE_TAG = "create";
public static final String SEARCH_TAG = "search";
public static final String PROFILE_TAG = "profile";


private MyFragmentTabHost tabHost;
private MyFragmentTabHost.TabInfo tabInfo;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_tab, viewGroup, false);

    tabHost = (MyFragmentTabHost) view.findViewById(android.R.id.tabhost);
    tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
    tabHost.addTab(tabHost.newTabSpec(HOME_TAG).setIndicator(getString(R.string.tab_home)), HomeFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec(CREATE_TAG).setIndicator(getString(R.string.tab_create)), CreateFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec(SEARCH_TAG).setIndicator(getString(R.string.tab_search)), CreateFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec(PROFILE_TAG).setIndicator(getString(R.string.tab_profile)), CreateFragment.class, null);

    MyFragmentTabHost.OnTabChangeListener tabListener = new MyFragmentTabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String s) {
            FragmentManager fm = getChildFragmentManager();
            HomeFragment homeFragment = (HomeFragment) fm.findFragmentByTag(HOME_TAG);
            CreateFragment createFragment = (CreateFragment) fm.findFragmentByTag(CREATE_TAG);

            FragmentTransaction ft = fm.beginTransaction();

            if(s.equalsIgnoreCase(HOME_TAG)){
                if(homeFragment == null)
                    ft.add(R.id.tabcontent, new HomeFragment(), HOME_TAG);
                else{
                    if(createFragment != null)
                        ft.hide(createFragment);
                    ft.show(homeFragment);
                }
            }
            else if(s.equalsIgnoreCase(CREATE_TAG)){
                if(createFragment == null)
                    ft.add(R.id.tabcontent, new CreateFragment(), CREATE_TAG);
                else{
                    if(homeFragment != null)
                        ft.hide(homeFragment);
                    ft.show(createFragment);
                }
            }

            ft.commit();
        }
    };

    tabHost.setOnTabChangedListener(tabListener);

    return view;
}
}

2 个答案:

答案 0 :(得分:1)

在FragmentTabHost中:

public class Message { 
     private String text; 
     public Message(String text) { 
          this.text = text;
     } 
     public int getLength() { 
          return text.length()
     } 
}

Transaction.attach(Fragment)将重新创建视图,请参阅源代码中的详细信息。 因此,您可以在您的片段中添加如下代码:

private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
    TabInfo newTab = null;
    for (int i=0; i<mTabs.size(); i++) {
        TabInfo tab = mTabs.get(i);
        if (tab.tag.equals(tabId)) {
            newTab = tab;
        }
    }
    if (newTab == null) {
        throw new IllegalStateException("No tab known for tag " + tabId);
    }
    if (mLastTab != newTab) {
        if (ft == null) {
            ft = mFragmentManager.beginTransaction();
        }
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext,
                        newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
    }
    return ft;
}

答案 1 :(得分:0)

每个切换FragmentTabHost片段都会调用onCreateView()来重绘用户界面。

FragmentTabHost.java中有一个方法私有:

FragmentTransaction doTabChanged(String tabId,  FragmentTransaction ft)
    private FragmentTransaction doTabChanged(String tabId,
        FragmentTransaction ft) {
    TabInfo newTab = null;
    for (int i = 0; i < mTabs.size(); i++) {
        TabInfo tab = mTabs.get(i);
        if (tab.tag.equals(tabId)) {
            newTab = tab;
        }
    }
    if (newTab == null) {
        throw new IllegalStateException("No tab known for tag " + tabId);
    }
    if (mLastTab != newTab) {
        if (ft == null) {
            ft = mFragmentManager.beginTransaction();
        }
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                **// hide replace of detach,hFragment
                // ft.detach(mLastTab.fragment);**
                ft.hide(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext,
                        newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
                **// show replace of attach,then show the Fragment
                // ft.attach(newTab.fragment);**
                ft.show(newTab.fragment);
            }
        }

        mLastTab = newTab;
    }
    return ft;
}

然后使用此FragmentTabHost代替support.v4.FragmentTabHost