使用片段为BottomNavigationView Android中的每个选项卡分隔Back Stack

使用片段为BottomNavigationView Android中的每个选项卡分隔Back Stack,第1张

概述我正在Android应用程序中实现BottomNavigationView导航.我正在使用Fragments为每个选项卡设置内容.我知道如何为每个选项卡设置一个片段,然后在单击选项卡时切换片段.但是,如何为每个选项卡分别设置一个后备堆栈?以下是设置一个片段的代码:FragmentselectedFragment=ItemsFrag

我正在Android应用程序中实现BottomNavigationVIEw导航.我正在使用Fragments为每个选项卡设置内容.

我知道如何为每个选项卡设置一个片段,然后在单击选项卡时切换片段.但是,如何为每个选项卡分别设置一个后备堆栈?
以下是设置一个片段的代码:

Fragment selectedFragment = ItemsFragment.newInstance();FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();transaction.replace(R.ID.content, selectedFragment);transaction.commit();

例如,片段A和B将位于选项卡1下,片段C和D位于选项卡2下.当应用程序启动时,将显示片段A并选择选项卡1.然后片段A可能被片段B替换.当选择标签2时,应显示片段C.如果选择了选项卡1,则应再次显示片段B.此时,应该可以使用后退按钮显示片段A.

以下是在同一选项卡中设置下一个片段的代码:

Fragment selectedFragment = ItemsFragment.newInstance();FragmentTransaction ft = getFragmentManager().beginTransaction();ft.replace(R.ID.content, selectedFragment);ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);ft.addToBackStack(null);ft.commit();

解决方法:

最后,我找到了解决方案,它的灵感来自之前对StackOverflow:Separate Back Stack for each tab in Android using Fragments的回答
 我只用BottomNavigationVIEw替换了TabHost,这里是代码:
 主要活动

public class MainActivity extends AppCompatActivity {private HashMap<String, Stack<Fragment>> mStacks;public static final String TAB_HOME  = "tab_home";public static final String TAB_DASHBOARD  = "tab_dashboard";public static final String TAB_NOTIFICATIONS  = "tab_notifications";private String mCurrentTab;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    BottomNavigationVIEw navigation = (BottomNavigationVIEw) findVIEwByID(R.ID.navigation);    navigation.setonNavigationItemSelectedListener(mOnNavigationItemSelectedListener);    mStacks = new HashMap<String, Stack<Fragment>>();    mStacks.put(TAB_HOME, new Stack<Fragment>());    mStacks.put(TAB_DASHBOARD, new Stack<Fragment>());    mStacks.put(TAB_NOTIFICATIONS, new Stack<Fragment>());    navigation.setSelectedItemID(R.ID.navigation_home);}private BottomNavigationVIEw.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener        = new BottomNavigationVIEw.OnNavigationItemSelectedListener() {    @OverrIDe    public boolean onNavigationItemSelected(@NonNull MenuItem item) {        switch (item.getItemID()) {            case R.ID.navigation_home:                selectedTab(TAB_HOME);                return true;            case R.ID.navigation_dashboard:                selectedTab(TAB_DASHBOARD);                return true;            case R.ID.navigation_notifications:                selectedTab(TAB_NOTIFICATIONS);                return true;        }        return false;    }};private voID gotoFragment(Fragment selectedFragment){    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();    fragmentTransaction.replace(R.ID.content, selectedFragment);    fragmentTransaction.commit();}private voID selectedTab(String tabID){    mCurrentTab = tabID;    if(mStacks.get(tabID).size() == 0){      /*       *    First time this tab is selected. So add first fragment of that tab.       *    Dont need animation, so that argument is false.       *    We are adding a new fragment which is not present in stack. So add to stack is true.       */        if(tabID.equals(TAB_HOME)){            pushFragments(tabID, new HomeFragment(),true);        }else if(tabID.equals(TAB_DASHBOARD)){            pushFragments(tabID, new DashboardFragment(),true);        }else if(tabID.equals(TAB_NOTIFICATIONS)){            pushFragments(tabID, new NotificationsFragment(),true);        }    }else {      /*       *    We are switching tabs, and target tab is already has atleast one fragment.       *    No need of animation, no need of stack pushing. Just show the target fragment       */        pushFragments(tabID, mStacks.get(tabID).lastElement(),false);    }}public voID pushFragments(String tag, Fragment fragment, boolean shouldAdd){    if(shouldAdd)        mStacks.get(tag).push(fragment);    FragmentManager manager = getSupportFragmentManager();    FragmentTransaction ft = manager.beginTransaction();    ft.replace(R.ID.content, fragment);    ft.commit();}public voID popFragments(){  /*   *    Select the second last fragment in current tab's stack..   *    which will be shown after the fragment transaction given below   */    Fragment fragment = mStacks.get(mCurrentTab).elementAt(mStacks.get(mCurrentTab).size() - 2);  /*pop current fragment from stack.. */    mStacks.get(mCurrentTab).pop();  /* We have the target fragment in hand.. Just show it.. Show a standard navigation animation*/    FragmentManager manager = getSupportFragmentManager();    FragmentTransaction ft = manager.beginTransaction();    ft.replace(R.ID.content, fragment);    ft.commit();}@OverrIDepublic voID onBackpressed() {    if(mStacks.get(mCurrentTab).size() == 1){        // We are already showing first fragment of current tab, so when back pressed, we will finish this activity..        finish();        return;    }    /* Goto prevIoUs fragment in navigation stack of this tab */    popFragments();}

}

家庭片段的例子

public class HomeFragment extends Fragment {@Nullable@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) {    VIEw vIEw = inflater.inflate(R.layout.fragment_home, container, false);    button gotoNextFragment = (button) vIEw.findVIEwByID(R.ID.gotoHome2);    gotoNextFragment.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw vIEw) {            ((MainActivity)getActivity()).pushFragments(MainActivity.TAB_HOME, new Home2Fragment(),true);        }    });    return vIEw;}

}

总结

以上是内存溢出为你收集整理的使用片段为BottomNavigationView Android中的每个选项卡分隔Back Stack全部内容,希望文章能够帮你解决使用片段为BottomNavigationView Android中的每个选项卡分隔Back Stack所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100604.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存