I have a Tablelayout with two tabs, in the code I wrote that the tabs were displayed when I clicked on them. Now it works like this: I open the activity and the fragments from the TabLayout are not displayed until I click on the title of the TabLayout header. How can I make it so that when I open the activity - fragment it is already visible?
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);
}
CodePudding user response:
You are selecting the tab without even adding it before. tabLayout.selectTab(tabLayout.getTabAt(0)); Move this code after adding tabs.
CodePudding user response:
The fact is that I did not create the default fragment in advance, before creating the TabLayout tabs. My solution:
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
}