要在Android Fragment之間進行切換,可以通過以下幾種方法:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new YourFragment();
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.commit();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment1 = fragmentManager.findFragmentByTag("Fragment1");
Fragment fragment2 = fragmentManager.findFragmentByTag("Fragment2");
if (fragment1.isHidden()) {
fragmentTransaction.show(fragment1);
fragmentTransaction.hide(fragment2);
} else {
fragmentTransaction.hide(fragment1);
fragmentTransaction.show(fragment2);
}
fragmentTransaction.commit();
ViewPager viewPager = findViewById(R.id.viewPager);
FragmentPagerAdapter adapter = new YourPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
以上是幾種常見的Android Fragment切換方法,根據實際需求選擇合適的方法進行切換。