bottomnavigationview用子片段替换片段 [英] bottomnavigationview Fragment replacement with sub-fragment

查看:80
本文介绍了bottomnavigationview用子片段替换片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有底部导航片段的应用程序,该片段由5个主要片段和一个额外的子片段(登录片段)组成。问题是,一旦用户成功登录,我希望将此登录片段替换为userAccount片段。

I have created an app with a bottom navigation fragment which consists of 5 main fragments and one extra sub fragment (login Fragment). the thing is I want this login fragment to be replaced with the userAccount fragment once user has logged in successfully.

注意:我首先静态地运行应用程序,所以我使用变量Boolean Called Status来检查用户是否已登录

Note: I am running the App statically at first so I am using a variable Boolean Called Status to check whether user has logged in or not

private static final boolean Status = false;

final Fragment f1 = new HomeFragment();
final Fragment f2 = new SearchFragment();
final Fragment f3 = new CameraFragment();
final Fragment f4 = new ChatFragment();


final Fragment f5 = new AccountFragment();


// logginFragment page should be replaced with AccountFragment once user logged in successfully
final Fragment f6 = new logginFragment();

private BottomNavigationViewEx.OnNavigationItemSelectedListener navListener =
        new BottomNavigationViewEx.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {


                switch (item.getItemId()) {

                    case R.id.nav_home:
                        fm.beginTransaction().hide(active).show(f1).commit();
                        active = f1;
                        return true;

                    case R.id.nav_search:
                        fm.beginTransaction().hide(active).show(f2).commit();
                        active = f2;
                        return true;
                    case R.id.nav_camera:
                        fm.beginTransaction().hide(active).show(f3).commit();
                        active = f3;
                        return true;
                    case R.id.nav_chat:
                        fm.beginTransaction().hide(active).show(f4).commit();
                        active = f4;
                        return true;
                    case R.id.nav_account:

                        fm.beginTransaction().hide(active).show(f5).commit();
                        active = f5;
                        return true;
                }
          /* \getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    selectedFragment).commit();*/


                return false;

            }
        };


推荐答案

仅当其中一个Bottom时,您的代码才更改片段导航栏项目被选中。您可以在登录成功时(即在点击按钮时)用帐户片段替换登录片段,并将其设置为活动,而不是监听选择的更改。让我知道是否有帮助。

Your Code changes the fragment only when one of the Bottom Navigation Bar item is selected. You can replace the login fragment with account fragment when the login is successful(i.e when button is tapped) and set it to active rather than listening for the change in selection. Let me know if this helps.

void login()
{
   if(Successful)
    {
      getSupportFragmentManager().beginTransaction()
                                .replace(R.id.fragment_login,R.id.fragment_account)
                                .commit(); 
    }
} 

这篇关于bottomnavigationview用子片段替换片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆