我正在使用NavigationDrawer和一个Activity,我正在替换FrameLayout中的Fragments。浏览NavigationDrawer并替换Activity内部的Fragments非常简单。当我从另一个片段替换片段时,我不确定如何处理。例如:
我展示了一个基本列表片段,当用户点击其中一个项目时,我会显示一个新的片段。现在,从这个新片段中,我可以选择显示另一个片段,这会产生更深层次的片段层次结构。我知道我可以通过
.addToBackStack()
,如果是后退按钮,请致电
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
来自Activity的,但随着Fragment层次结构越深入,它变得越复杂和越脏。是否有一篇好文章或建筑方法来处理像这样的案例?
答案 0 :(得分:1)
在您的方案中,您应该使用嵌套片段。 您可以将片段嵌入片段中。
要嵌套片段,只需在要添加片段的getChildFragmentManager()
上调用Fragment
即可。这将返回一个FragmentManager
,您可以像往常一样从顶级活动中创建片段事务。例如,这里有一些代码可以在现有的Fragment
类中添加片段:
Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
在嵌套片段中,您可以通过调用getParentFragment().
来获取对父片段的引用,然后您的父背斜和子堆栈将被分开。
这是怎么做的。另一个问题是你应该考虑使用intent
和新活动而不是10个嵌套片段?这是一篇关于子片段的文章。link
Stack similar question
答案 1 :(得分:0)
在主要活动中制作片段标签:
//----------------Fragment TAGS-----------------------\\
private final String FRAGMENT_BOOK_RIDE = "Frag Book Ride";
private final String FRAGMENT_RIDE_HISTORY = "Frag Ride History";
private final String FRAGMENT_PAYMENT = "Frag Payment";
private final String FRAGMENT_PROFILES_SETTINGS = "Frag Profile & Settings";
private final String FRAGMENT_HELP = "Frag Help";
//-----------------------------------------------------\\
导航项目选择:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here. \\
int id = item.getItemId();
switch (id) {
case R.id.nav_book_ride:
removeAllFragments();
getSupportActionBar().setTitle(getResources().getString(R.string.app_name));
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
break;
case R.id.nav_ride_history:
removeAllFragments();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new RideHistoryFragment(getApplicationContext())
, FRAGMENT_RIDE_HISTORY)
.commit();
break;
case R.id.nav_payment:
removeAllFragments();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new PaymentFragment(getApplicationContext())
, FRAGMENT_PAYMENT)
.commit();
break;
case R.id.nav_settings:
removeAllFragments();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new ProfileSettingFragment(getApplicationContext())
, FRAGMENT_PROFILES_SETTINGS)
.commit();
break;
case R.id.nav_help:
removeAllFragments();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new HelpFragment(getApplicationContext())
, FRAGMENT_HELP)
.commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
处理backKey按:
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
backKeyMethods();
}
}
/**
* Custom back key method for managing fragment back key behavior
*/
private void backKeyMethods() {
RideHistoryFragment rideHistoryFragment = (RideHistoryFragment) getSupportFragmentManager().
findFragmentByTag(FRAGMENT_RIDE_HISTORY);
PaymentFragment paymentFragment = (PaymentFragment) getSupportFragmentManager().
findFragmentByTag(FRAGMENT_PAYMENT);
HelpFragment helpFragment = (HelpFragment) getSupportFragmentManager().
findFragmentByTag(FRAGMENT_HELP);
ProfileSettingFragment userProfileFragment = (ProfileSettingFragment) getSupportFragmentManager().
findFragmentByTag(FRAGMENT_PROFILES_SETTINGS);
if (rideHistoryFragment != null && rideHistoryFragment.isVisible()) {
navigationView.setCheckedItem(R.id.nav_book_ride);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
} else if (paymentFragment != null && paymentFragment.isVisible()) {
navigationView.setCheckedItem(R.id.nav_book_ride);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
} else if (userProfileFragment != null && userProfileFragment.isVisible()) {
navigationView.setCheckedItem(R.id.nav_book_ride);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
} else if (helpFragment != null && helpFragment.isVisible()) {
navigationView.setCheckedItem(R.id.nav_book_ride);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
, FRAGMENT_BOOK_RIDE)
.commit();
} else if (getFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
/**
* Remove all fragments in Fragment BackStack
*/
private void removeAllFragments() {
FragmentManager fm = getSupportFragmentManager();
for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}
}
每当你想要深入时使用addToBackStack(null),所以当你按下backKey时它将立即弹出片段。 希望它有所帮助。