【问题标题】:Fragment having blank content at initial startup or after returning from another activity在初始启动或从另一个活动返回后具有空白内容的片段
【发布时间】:2018-07-15 05:03:18
【问题描述】:

我有一个包含两个选项卡(第一个选项卡,第二个选项卡)的应用程序。但是在启动或从另一个活动返回期间,第一个选项卡的片段是空白的。仅在我单击第二个选项卡然后单击第一个选项卡后才会显示该片段。 另一个问题,有没有办法在初始启动时显示第二个选项卡而不是第一个选项卡?

    //
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_tab_base);

    simpleFrameLayout = findViewById(R.id.simpleFrameLayout);
    tabLayout = findViewById(R.id.simpleTabLayout);

    TabLayout.Tab firstTab = tabLayout.newTab();
    firstTab.setText("Income");
    tabLayout.addTab(firstTab);

    TabLayout.Tab secondTab = tabLayout.newTab();
    secondTab.setText("Expenses");
    tabLayout.addTab(secondTab);

    // I try to set the fragment but startup but no effect
    if (fragment==null)
        fragment = new IncomeTab();

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()       {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switch (tab.getPosition()) {
                case 0:
                    fragment = new IncomeTab();
                    break;
                case 1:
                    fragment = new ExpenseTab();
                    break;
            }
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.simpleFrameLayout, fragment);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.commit();
        }            
    });
}

【问题讨论】:

    标签: java android tabs fragment


    【解决方案1】:

    它为空白的原因仅仅是因为片段尚未显示。

    只有在您提交将 Fragment 放置到屏幕上的 FragmentTransaction 后才会显示 Fragment,但是,在您的代码中,这只发生在按下 Tab 时。因此,在您按下选项卡之前,不会显示任何片段。

    // I try to set the fragment but startup but no effect
    if (fragment==null)
        fragment = new IncomeTab();
    

    这段代码也是无用的,因为它不是通过 FragmentTransaction 完成的,您只是创建了一个新的 IncomeTab 片段并且什么都不做。

    修复如下:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_tab_base);
    
        simpleFrameLayout = findViewById(R.id.simpleFrameLayout);
        tabLayout = findViewById(R.id.simpleTabLayout);
    
        TabLayout.Tab firstTab = tabLayout.newTab();
        firstTab.setText("Income");
        tabLayout.addTab(firstTab);
    
        TabLayout.Tab secondTab = tabLayout.newTab();
        secondTab.setText("Expenses");
        tabLayout.addTab(secondTab);
    
        replaceFragment(1);
    
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                replaceFragment(int tabPosition)
            }            
        });
    }
    
    private void replaceFragment(int tabPosition){
        switch (tab.getPosition()) {
            case 0:
                fragment = new IncomeTab();
                break;
            case 1:
                fragment = new ExpenseTab();
                break;
        }
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.simpleFrameLayout, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();
    }
    

    通过确保在该片段上执行 FragmentTransaction,您可以在启动应用程序时显示您想要的任何片段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多