我有一个包含FragmentTabHost的片段。我把它设置在这里:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_settings_organization, container, false);
Log.v(TabHome.TAG, "SettingsOrganization onCreateView()");
setUpActionBar();
TextView headerView =(TextView) v.findViewById(R.id.orgNameHeader);
headerView.setText(organization.name);
// showSpinner(true);
mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
mTabHost.setup(activity, this.getChildFragmentManager(), R.id.realtabcontent);
mTabHost.setVisibility(View.INVISIBLE);
mTabHost.addTab(
mTabHost.newTabSpec("text")
.setIndicator(getString(R.string.settings_text_select), activity.getResources().getDrawable(R.drawable.ic_home)),
SettingsOrganizationTabFragment_.class, null
);
mTabHost.addTab(
mTabHost.newTabSpec("voice")
.setIndicator(getString(R.string.settings_voice_select), activity.getResources().getDrawable(R.drawable.ic_home)),
SettingsOrganizationTabFragment_.class, null
);
mTabHost.addTab(
mTabHost.newTabSpec("app")
.setIndicator(getString(R.string.settings_app_select), activity.getResources().getDrawable(R.drawable.ic_home)),
SettingsOrganizationTabFragment_.class, null
);
restClient = new RestClient_();
SettingsOrganizationErrorHandler errorHandler = new SettingsOrganizationErrorHandler();
restClient.setRestErrorHandler(errorHandler);
RestTemplate restTemplate = restClient.getRestTemplate();
ClientHttpRequestFactory factory = RestClientHelper.clientHttpRequestFactory();
restTemplate.setRequestFactory(factory);
RestClientHelper.prepareRestClient(restClient);
restClient.setHeader("Auth-Token",TabHome.authToken);
GetSettingsTask task = new GetSettingsTask();
task.execute();
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("text"));
}
return v;
}
您还可以在此处看到我启动后台任务来获取一些数据。完成后,我需要更新所有三个选项卡。问题是只存在第一个选项卡;其他人都是空的。
我还尝试更新onTabChanged()中的其他选项卡,但在这种情况下,当frag不是第一个选项卡时,frag为null:
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Log.v(TabHome.TAG,"Tab ID:"+ tabId);
SettingsOrganizationTabFragment frag = (SettingsOrganizationTabFragment)thisFrag.getChildFragmentManager().findFragmentByTag(tabId);
if (tabId.equalsIgnoreCase("text")){
frag.notificationType=SettingsOrganizationTabFragment.NOTIFICATION_TYPE_TEXT;
frag.settings =settings;
frag.updateView();
}
if(tabId.equalsIgnoreCase("voice")){
frag.notificationType=SettingsOrganizationTabFragment.NOTIFICATION_TYPE_VOICE;
frag.settings =settings;
frag.updateView();
}
if(tabId.equalsIgnoreCase("app")){
frag.notificationType=SettingsOrganizationTabFragment.NOTIFICATION_TYPE_APP;
frag.settings =settings;
frag.updateView();
}
}
});
(thisFrag指的是包含fragmentTabHost的片段)
如果其他标签尚未显示,我该如何更新?
谢谢!
答案 0 :(得分:0)
执行此操作的唯一方法是创建选项卡,如果它们仍然为null,或使用MVC模式中的模型。我建议第二种选择。实际创建选项卡后,您将从模型中获取选项卡所需的信息。
public class SecondTab {
private Model model;
private String infoS;
private int infoI;
private boolean infoB;
public SecondTab(Model model) {
this.model = model;
infoS = model.getInfoS();
infoI = model.getInfoI();
infoB = model.getInfoB
}
public void editInfoS(String s){
model.setInfoS(s);
}
}
然后您将传递,然后在更新选项卡时将更新的信息添加到模型中。实际创建选项卡时,只需将模型传递给它。
答案 1 :(得分:0)
我最终使用了我的标签片段的onResume()方法,每次选择标签时都会调用该方法。我到达并获取父片段(具有我需要的获取数据),然后相应地更新选项卡,如果数据存在。
@Override
public void onResume(){
Log.v(TabHome.TAG, "SettingsOrganizationTab - onResume");
super.onResume();
TabHome.activeFragment = this;
activity.supportInvalidateOptionsMenu();
parent = (SettingsOrganizationFragment)this.getParentFragment();
Log.v(TabHome.TAG,"Selected Tab: "+parent.mTabHost.getCurrentTab());
notificationType = parent.mTabHost.getCurrentTab();
if (parent.settings!=null){
updateView();
}
}