简体   繁体   English

使用按钮 onClick 在片段之间切换,但底部导航图标不会改变

[英]Switching between fragments with button onClick but bottom navigation icon does not change

I have an app which has food menu and cart in the bottom navigation and used fragments for those.我有一个应用程序,它在底部导航中有食物菜单和购物车,并为它们使用了片段。 There is a button in the cart fragment which will show when the cart is empty.购物车片段中有一个按钮,当购物车为空时会显示。 This is a 'Add Food' button because the cart is empty.这是一个“添加食物”按钮,因为购物车是空的。 This button takes the user to the menu fragment.此按钮将用户带到菜单片段。 That part of the code works, but my issue is that the icon in the bottom navigation bar is not changing from cart icon to menu icon.这部分代码有效,但我的问题是底部导航栏中的图标没有从购物车图标更改为菜单图标。

public class CartFragment extends Fragment {

private LinearLayout emptyCartInfo;
private Button addFoodButton;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_cart, container, false);
    setHasOptionsMenu(true);
    //return inflater.inflate(R.layout.fragment_cart, container, false);

    emptyCartInfo = view.findViewById(R.id.cart_empty_layout);
    addFoodButton = view.findViewById(R.id.add_food_button);
    addFoodButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Fragment menu = new MenuFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.fragment_container, menu);
            ft.commit();


        }
    });

    return view;
}

} }

The code above works fine on click of the button which takes me to menu fragment, however, the icon shown in the bottom navigation remains selected as cart and I need help as how to change this from within the fragment.上面的代码在单击将我带到菜单片段的按钮时可以正常工作,但是,底部导航中显示的图标仍然选择为购物车,我需要帮助来了解如何从片段中更改它。 When fragment switches from cart to menu, icon in the bottom navigation should also change which in my case does not.当片段从购物车切换到菜单时,底部导航中的图标也应该改变,在我的情况下不会改变。 Need to do this from within the cart fragment under the onClick method.需要在 onClick 方法下的购物车片段中执行此操作。

In your onClick function, you should add a statement that turns off the focus from one MenuItem by changing the colour: (In case that menu is already defined)在您的 onClick function 中,您应该添加一条语句,通过更改颜色来关闭一个 MenuItem 的焦点:(如果该菜单已定义)

menu.findItem(R.id.main_page).setIconColor(Color.WHITE);

And the same thing in the second Fragment, while resuming it, change the colour to your "focused" colour.第二个片段中的相同内容,在恢复它的同时,将颜色更改为您的“聚焦”颜色。

Also, I'd like to suggest, just because I think it would be a better UX, changing the bottom navigation to a tabbed menu (TabbedLayout).另外,我想建议,只是因为我认为它会是一个更好的用户体验,将底部导航更改为选项卡式菜单(TabbedLayout)。 Then, just remove the button and you'll be able to swipe between Fragments.然后,只需删除按钮,您就可以在片段之间滑动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM