【发布时间】:2018-07-14 08:42:16
【问题描述】:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen_bottombar);
BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
initBottomBar(bottomNavigationView);}
private void initBottomBar(BottomNavigationView bottomNavigationView) {
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
fragmentTag = "home";
savedFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (savedFragment == null)
savedFragment = NeedyFeedyHomeTabFragment.getInstance();
break;
case R.id.search:
fragmentTag = "search";
savedFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (savedFragment == null) {
savedFragment = SearchResultFragment.getInstance();
}
break;
case R.id.profile:
fragmentTag = "profile";
savedFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (savedFragment == null) {
savedFragment = FragmentProfileDetails.getInstance();
}
break;
case R.id.favourite:
fragmentTag = "favourite";
savedFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (savedFragment == null) {
savedFragment = FavouriteListFragment.getInstance();
}
break;
}
fragmentTransactionExecution(savedFragment, fragmentTag);
return true;
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, NeedyFeedyHomeTabFragment.getInstance(), "home");
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
}
private void fragmentTransactionExecution(Fragment selectedFragment, String fragmentTag) {
if (selectedFragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment, fragmentTag);
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
}
}
【问题讨论】:
-
大家好,请建议我如何避免在单击选项卡时重新创建片段
-
您好,请正确格式化您的代码。事实上,人们很难看到它并帮助您解决问题。
-
标签是你在创建片段时设置的,请发布你的代码来解释你是如何创建片段实例的。
标签: android fragment bottomnavigationview bottombar