我有一个带有两个选项卡(第一个选项卡,第二个选项卡)的应用程序。但是,在启动或从另一个活动返回时,第一个选项卡的片段为空白。只有在我单击第二个选项卡然后单击第一个选项卡后,才会显示该片段。另一个问题,是否有办法在初次启动时显示第二个选项卡而不是第一个选项卡? //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(); } });}
添加回答
举报
0/150
提交
取消