问题描述
结果
我想用code来源:
http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html ,但我看到,它的创造和放大器;每一个交换机之间的删除片段。结果你能告诉我怎么改,所以每一个片段将在创建之初只有一次或后先进入呢?结果谢谢!
P.S。我使用与TabHost片段被用于显示放置在它里面的其他片段中的一个。
更新:
在(子)我得到 onCreateView
和 onActivityCreated
方法片段切换从一个标签到另一个后,每一次。是不是该线路原因引起的?
如果(mLastTab!= NULL)
{
如果(mLastTab.fragment!= NULL)
{
ft.detach(mLastTab.fragment);
}
}
有关TabManager全部源$ C $ C(片段放在里面的子类):
公共类TabManager实现TabHost.OnTabChangeListener
{
私人最终TabHost mTabHost;
私人最终诠释mContainerId;
私人最终的HashMap<弦乐,TabInfo> mTabs =新的HashMap<弦乐,TabInfo>();
TabInfo mLastTab; 最后一类TabInfo
{
私人最终字符串标记;
私人最终级<> CLSS;
私人最终捆绑ARGS;
私人片段片段; TabInfo(字符串_tag,类<> _class,捆绑_args)
{
标签= _tag;
CLSS = _class;
ARGS = _args;
}
} 类DummyTabFactory实现TabHost.TabContentFactory
{
私人最终上下文mContext; 公共DummyTabFactory(上下文的背景下)
{
mContext =背景;
} @覆盖
公共查看createTabContent(字符串标签)
{
视图V =新景(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
返回伏;
}
} 公共TabManager(TabHost tabHost,诠释containerId)
{
mTabHost = tabHost;
mContainerId = containerId;
mTabHost.setOnTabChangedListener(本);
} 公共无效addTab(TabHost.TabSpec则tabspec,类<> CLSS,捆绑参数)
{
tabSpec.setContent(新DummyTabFactory(getActivity()));
字符串标记= tabSpec.getTag(); TabInfo信息=新TabInfo(标签,CLSS,参数); //检查,看看我们是否已经为这个标签片段,可能
//从previously保存的状态。如果是这样,禁用它,因为我们
//初始状态是一个选项卡不显示。
info.fragment = getFragmentManager()findFragmentByTag(标签)。
如果(info.fragment = NULL&放大器;!&安培;!info.fragment.isDetached())
{
FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
ft.detach(info.fragment);
ft.commit();
} mTabs.put(标签,信息);
mTabHost.addTab(则tabspec);
} @覆盖
公共无效onTabChanged(字符串tabId)
{
TabInfo newTab = mTabs.get(tabId);
如果(mLastTab!= newTab)
{
FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
如果(mLastTab!= NULL)
{
如果(mLastTab.fragment!= NULL)
{
ft.detach(mLastTab.fragment);
}
}
如果(newTab!= NULL)
{
如果(newTab.fragment == NULL)
{
newTab.fragment = Fragment.instantiate(getActivity(),newTab.clss.getName(),newTab.args);
ft.add(mContainerId,newTab.fragment,newTab.tag);
}其他
{
ft.attach(newTab.fragment);
}
} mLastTab = newTab;
ft.commit();
getFragmentManager()executePendingTransactions()。
}
}
}
编辑:
我展示/点击标签,而不是安装/拆卸后隐藏片段。不知道这是正确的,但。
解决方法:
公共类TabManager实现TabHost.OnTabChangeListener
{
私人最终TabHost mTabHost;
私人最终诠释mContainerId;
私人最终的HashMap<弦乐,TabInfo> mTabs =新的HashMap<弦乐,TabInfo>();
TabInfo mLastTab; 最后一类TabInfo
{
私人最终字符串标记;
私人最终级<> CLSS;
私人最终捆绑ARGS;
私人片段片段; TabInfo(字符串_tag,类<> _class,捆绑_args)
{
标签= _tag;
CLSS = _class;
ARGS = _args;
}
} 类DummyTabFactory实现TabHost.TabContentFactory
{
私人最终上下文mContext; 公共DummyTabFactory(上下文的背景下)
{
mContext =背景;
} @覆盖
公共查看createTabContent(字符串标签)
{
视图V =新景(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
返回伏;
}
} 公共TabManager(TabHost tabHost,诠释containerId)
{
mTabHost = tabHost;
mContainerId = containerId;
mTabHost.setOnTabChangedListener(本);
} 公共无效addTab(TabHost.TabSpec则tabspec,类<> CLSS,捆绑参数)
{
tabSpec.setContent(新DummyTabFactory(getActivity()));
字符串标记= tabSpec.getTag(); TabInfo信息=新TabInfo(标签,CLSS,参数); //检查,看看我们是否已经为这个标签片段,可能
//从previously保存的状态。如果是这样,禁用它,因为我们
//初始状态是一个选项卡不显示。
info.fragment = getFragmentManager()findFragmentByTag(标签)。
如果(info.fragment = NULL&放大器;!&安培;!info.fragment.isDetached())
{
FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
ft.detach(info.fragment);
ft.commit();
} mTabs.put(标签,信息);
mTabHost.addTab(则tabspec);
} @覆盖
公共无效onTabChanged(字符串tabId)
{
TabInfo newTab = mTabs.get(tabId);
如果(mLastTab!= newTab)
{
android.support.v4.app.FragmentTransaction英尺=((FragmentActivity)getActivity())getSupportFragmentManager()的BeginTransaction()。;
如果(mLastTab!= NULL)
{
如果(mLastTab.fragment!= NULL)
{
ft.hide(mLastTab.fragment); }
}
如果(newTab!= NULL)
{
如果(newTab.fragment == NULL)
{
newTab.fragment = Fragment.instantiate(getActivity(),newTab.clss.getName(),newTab.args);
ft.add(mContainerId,newTab.fragment,newTab.tag);
}其他
{
ft.show(newTab.fragment);
}
} mLastTab = newTab;
ft.commit();
getFragmentManager()executePendingTransactions()。
}
}
}
它不创建一个新的碎片每一次,它只做如果片段没有连接到标签,即newTab.fragment在onTabChanged方法无效
如果(newTab.fragment == NULL){
newTab.fragment = Fragment.instantiate(mActivity,
newTab.clss.getName(),newTab.args);
ft.add(mContainerId,newTab.fragment,newTab.tag);
}其他{
ft.attach(newTab.fragment);
}
I want to use code from :http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html , but I see that it's creating&deleting fragments between every switch.
Could you tell me how to change it, so every fragment would be created only once at beginning or after first enter into it?
Thanks!
P.S. I am using Fragment with TabHost which is used to display one of other Fragments placed inside it.
Update:I am getting onCreateView
and onActivityCreated
methods in (sub)Fragments every time after switching from one tab to another. Isn't it caused by this lines?
if (mLastTab != null)
{
if (mLastTab.fragment != null)
{
ft.detach(mLastTab.fragment);
}
}
Whole source code for TabManager (placed inside Fragment subclass) :
public class TabManager implements TabHost.OnTabChangeListener
{
private final TabHost mTabHost;
private final int mContainerId;
private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
TabInfo mLastTab;
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;
}
}
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(TabHost tabHost, int containerId)
{
mTabHost = tabHost;
mContainerId = containerId;
mTabHost.setOnTabChangedListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
{
tabSpec.setContent(new DummyTabFactory(getActivity()));
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 = getFragmentManager().findFragmentByTag(tag);
if (info.fragment != null && !info.fragment.isDetached())
{
FragmentTransaction ft = getFragmentManager().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);
if (mLastTab != newTab)
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (mLastTab != null)
{
if (mLastTab.fragment != null)
{
ft.detach(mLastTab.fragment);
}
}
if (newTab != null)
{
if (newTab.fragment == null)
{
newTab.fragment = Fragment.instantiate(getActivity(), newTab.clss.getName(), newTab.args);
ft.add(mContainerId, newTab.fragment, newTab.tag);
} else
{
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
getFragmentManager().executePendingTransactions();
}
}
}
EDIT:I'm showing/hiding fragment after clicking on tab instead of attaching/detaching. Don't know if this is correct though. Solution:
public class TabManager implements TabHost.OnTabChangeListener
{
private final TabHost mTabHost;
private final int mContainerId;
private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
TabInfo mLastTab;
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;
}
}
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(TabHost tabHost, int containerId)
{
mTabHost = tabHost;
mContainerId = containerId;
mTabHost.setOnTabChangedListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
{
tabSpec.setContent(new DummyTabFactory(getActivity()));
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 = getFragmentManager().findFragmentByTag(tag);
if (info.fragment != null && !info.fragment.isDetached())
{
FragmentTransaction ft = getFragmentManager().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);
if (mLastTab != newTab)
{
android.support.v4.app.FragmentTransaction ft = ((FragmentActivity) getActivity()).getSupportFragmentManager().beginTransaction();
if (mLastTab != null)
{
if (mLastTab.fragment != null)
{
ft.hide(mLastTab.fragment);
}
}
if (newTab != null)
{
if (newTab.fragment == null)
{
newTab.fragment = Fragment.instantiate(getActivity(), newTab.clss.getName(), newTab.args);
ft.add(mContainerId, newTab.fragment, newTab.tag);
} else
{
ft.show(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
getFragmentManager().executePendingTransactions();
}
}
}
It doesn't create a new fragment every time, it only does if the fragment is not attached to the tab i.e newTab.fragment is null in the onTabChanged method
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);
}
这篇关于FragmentTabs创造新的片段对象每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!