【问题标题】:Android: FragmentTabHost null FragmentsAndroid:FragmentTabHost 空片段
【发布时间】:2014-04-09 20:33:50
【问题描述】:

我有一个包含 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 不是第一个选项卡时,它为 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的片段)

即使其他标签尚未显示,我如何更新它们?

谢谢!

【问题讨论】:

    标签: android tabs android-tabhost android-tabs fragment-tab-host


    【解决方案1】:

    唯一的方法是在标签仍然为空时创建标签,或者使用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);
        }
    }
    

    然后,当您的选项卡应该更新时,您将通过然后将更新的信息添加到您的模型中。当您实际创建选项卡时,您只需将模型传递给它。

    【讨论】:

    • 你能提供一个我将如何创建标签的例子吗?
    【解决方案2】:

    我最终使用了标签片段的 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();
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2014-10-13
      • 2021-05-13
      相关资源
      最近更新 更多