我有一個帶有兩個選項卡的 Tablelayout,在我撰寫的代碼中,當我單擊它們時會顯示這些選項卡。現在它是這樣作業的:我打開活動,直到我點擊 TabLayout 標題的標題,TabLayout 中的片段才會顯示。我怎樣才能讓它在我打開活動時 - 片段已經可見?
public class DetailActivity extends AppCompatActivity {
FrameLayout simpleFrameLayout;
TabLayout tabLayout;
ImageView onBackPressed;
Button subsButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
subsButton = findViewById(R.id.subsButton);
subsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (subsButton.getText().equals("Подписаться")) {
subsButton.setText("Отписаться");
subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorGrey));
} else if (subsButton.getText().equals("Отписаться")) {
subsButton.setText("Подписаться");
subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorPrimary));
}
}
});
simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
onBackPressed = (ImageView) findViewById(R.id.onBackPressed);
tabLayout.selectTab(tabLayout.getTabAt(0));
TabLayout.Tab firstTab = tabLayout.newTab();
firstTab.setText("Chelsea");
tabLayout.addTab(firstTab);
TabLayout.Tab secondTab = tabLayout.newTab();
secondTab.setText("Manchester City");
tabLayout.addTab(secondTab);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Fragment fragment = null;
switch (tab.getPosition()) {
case 0:
fragment = new FragmentHisOne();
break;
case 1:
fragment = new FragmentHisTwo();
break;
default:
fragment = new FragmentHisOne();
break;
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.simpleFrameLayout, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void onBackPressed(View view) {
Intent intent = new Intent(view.getContext(), MainActivity.class);
view.getContext().startActivity(intent);
}
uj5u.com熱心網友回復:
您在選擇選項卡之前甚至沒有添加它。tabLayout.selectTab(tabLayout.getTabAt(0)); 添加選項卡后移動此代碼。
uj5u.com熱心網友回復:
事實是,在創建 TabLayout 選項卡之前,我沒有提前創建默認片段。我的解決方案:
FrameLayout simpleFrameLayout;
TabLayout tabLayout;
ImageView onBackPressed;
Button subsButton;
Fragment fragment = null;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
subsButton = findViewById(R.id.subsButton);
subsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (subsButton.getText().equals("Подписаться")) {
subsButton.setText("Отписаться");
subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorGrey));
} else if (subsButton.getText().equals("Отписаться")) {
subsButton.setText("Подписаться");
subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorPrimary));
}
}
});
simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
onBackPressed = (ImageView) findViewById(R.id.onBackPressed);
tabLayout.selectTab(tabLayout.getTabAt(0));
TabLayout.Tab firstTab = tabLayout.newTab();
firstTab.setText("Chelsea");
tabLayout.addTab(firstTab);
TabLayout.Tab secondTab = tabLayout.newTab();
secondTab.setText("Manchester City");
tabLayout.addTab(secondTab);
fragment = new FragmentHisOne();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.simpleFrameLayout, fragment);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Fragment fragment = null;
switch (tab.getPosition()) {
case 0:
fragment = new FragmentHisOne();
break;
case 1:
fragment = new FragmentHisTwo();
break;
default:
fragment = new FragmentHisOne();
break;
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.simpleFrameLayout, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void onBackPressed(View view) {
Intent intent = new Intent(view.getContext(), MainActivity.class);
view.getContext().startActivity(intent);
}
public void subsButton(View view) {
//Button.setBackground
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/324316.html
標籤:爪哇 安卓 科特林 android-fragments android-tablayout
上一篇:Android:工具列中的泄漏
