将片段嵌套在底部导航视图中

时间:2018-02-01 17:15:59

标签: android android-fragments

我在主活动中有一个底部导航视图,有三个片段,A,B和C.我想在我的B片段中嵌套另一个片段,但也要确保你仍然可以导航回B片段,到A和C(使用底部导航视图)。

我尝试在我的B中嵌套一个片段,但它给了我一个错误:

rhythmTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        if (isChecked) {
            android.support.v4.app.FragmentTransaction ft = getChildFragmentManager().beginTransaction();
            RhythmTwo rhythmtwo = new RhythmTwo();
            ft.replace(R.id.frame2, rhythmtwo);
            ft.commit();
        } else {

        }
    }
});  

logcat的:

  

了java.lang.RuntimeException:   com.stefanawesome.piano.MainActivity@76a0944必须实施   OnFragmentInteractionListener

即使这样可行,我也不确定底部导航视图会做什么,我想确保你仍然可以回到片段B到片段A和C.任何帮助?

1 个答案:

答案 0 :(得分:0)

将此代码放入您的片段,当您想要通过片段转到另一个片段时:

 //signIn Screen call
    signIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((MainActivity) getActivity()).login();
        }
    });

将此代码放入您拥有所有三个片段的Activity:

 public void login()
{
    LoginFragment loginFragment = LoginFragment.newInstance();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame_home, loginFragment)
            .commit();
    toolbarTop.setVisibility(View.VISIBLE);
    mTitle.setText("Sign In");

}

就像我在同一活动的底部菜单中还有五个碎片一样:

 @Override
public void onClick(View v) {
    if(v.getId()== R.id.home_ll){
        homeImg.setImageResource(R.mipmap.ic_home_active);
        searchImg.setImageResource(R.mipmap.ic_search);
        categoriesImg.setImageResource(R.mipmap.ic_categories);
        cartImg.setImageResource(R.mipmap.ic_cart);
        accountImg.setImageResource(R.mipmap.ic_account);
      //  homeImg.setColorFilter(ContextCompat.getColor(this,R.color.colorPrimary));

        mTitle.setText("Bloom");
        toolbarTop.setVisibility(View.VISIBLE);
        HomeFragment homeFragment = new HomeFragment().newInstance();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame_home, homeFragment)
                .commit();


    }
    if(v.getId()== R.id.search_ll){
        homeImg.setImageResource(R.mipmap.ic_home);
        searchImg.setImageResource(R.mipmap.ic_search_active);
        categoriesImg.setImageResource(R.mipmap.ic_categories);
        cartImg.setImageResource(R.mipmap.ic_cart);
        accountImg.setImageResource(R.mipmap.ic_account);

        mTitle.setText("Search");
        toolbarTop.setVisibility(View.VISIBLE);
        SearchFragment searchFragment = new SearchFragment().newInstance();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame_home, searchFragment)
                .commit();






    }
    if(v.getId()== R.id.categories_ll){
        homeImg.setImageResource(R.mipmap.ic_home);
        searchImg.setImageResource(R.mipmap.ic_search);
        categoriesImg.setImageResource(R.mipmap.ic_categories_active);
        cartImg.setImageResource(R.mipmap.ic_cart);
        accountImg.setImageResource(R.mipmap.ic_account);


        mTitle.setText("Categories");
        toolbarTop.setVisibility(View.VISIBLE);
        CategoriesFragment categoriesFragment = new CategoriesFragment().newInstance();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame_home, categoriesFragment)
                .commit();


    }
    if(v.getId()== R.id.cart_ll){
        homeImg.setImageResource(R.mipmap.ic_home);
        searchImg.setImageResource(R.mipmap.ic_search);
        categoriesImg.setImageResource(R.mipmap.ic_categories);
        cartImg.setImageResource(R.mipmap.ic_cart_active);
        accountImg.setImageResource(R.mipmap.ic_account);

        mTitle.setText("Cart");
        toolbarTop.setVisibility(View.VISIBLE);
        CartFragment cartFragment = new CartFragment().newInstance();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame_home, cartFragment)
                .commit();




    }
    if(v.getId()== R.id.account_ll){
        homeImg.setImageResource(R.mipmap.ic_home);
        searchImg.setImageResource(R.mipmap.ic_search);
        categoriesImg.setImageResource(R.mipmap.ic_categories);
        cartImg.setImageResource(R.mipmap.ic_cart);
        accountImg.setImageResource(R.mipmap.ic_account_active);

        if(pref.getBoolean("login_status", false))
        {
            mTitle.setText("Account");
            toolbarTop.setVisibility(View.VISIBLE);

            Log.e("LOGIN","Yes");
            AccountFragment accountFragment = new AccountFragment().newInstance();
            getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame_home, accountFragment)
                .commit();
        }
        else
        {
            mTitle.setText("Sign In");
            toolbarTop.setVisibility(View.VISIBLE);

            Log.e("LOGIN","No");
            LoginFragment loginFragment = new LoginFragment().newInstance();
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.content_frame_home, loginFragment)
                    .commit();
        }



    }

}