猿问

BottomNavigationView 的菜单未选择

当我刚刚单独创建BottomNavigationView 时效果很好。但是将 BottomNavigationView 与 Fragment 一起使用有问题。问题是 Fragment 变化良好,但 BottomNavigationView 菜单固定在第一个菜单上。


我需要在哪里设置编码/字符集?(对不起,我的英语不好...)


package com.example.ui;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import com.google.android.material.bottomnavigation.BottomNavigationView;


public class HomeActivity extends AppCompatActivity implements View.OnClickListener{


    private FragmentManager fragmentManager = getSupportFragmentManager();

    private menu1Fragment menu1Fragment = new menu1Fragment();

    private menu2Fragment menu2Fragment = new menu2Fragment();

    private menu3Fragment menu3Fragment = new menu3Fragment();

    private menu4Fragment menu4Fragment = new menu4Fragment();


    @Override

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        getSupportActionBar().setIcon(R.drawable.logo);

        getSupportActionBar().setDisplayShowHomeEnabled(true);

        setContentView(R.layout.activity_home);


        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);



        FragmentTransaction transaction = fragmentManager.beginTransaction();

        transaction.replace(R.id.frame_home, menu1Fragment).commitAllowingStateLoss();



        bottomNavigationView.setOnNavigationItemSelectedListener(new ItemSelectedListener());

    }


    class ItemSelectedListener implements BottomNavigationView.OnNavigationItemSelectedListener {

        @Override

        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            FragmentTransaction transaction = fragmentManager.beginTransaction();


            switch (menuItem.getItemId()) {

                case R.id.navigation_menu1:

            }

            return false;

        }

    }



陪伴而非守候
浏览 166回答 2
2回答

达令说

尝试像这样最后返回 true class ItemSelectedListener implements BottomNavigationView.OnNavigationItemSelectedListener {    @Override    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {        FragmentTransaction transaction = fragmentManager.beginTransaction();        switch (menuItem.getItemId()) {            case R.id.navigation_menu1:                transaction.replace(R.id.frame_home, menu1Fragment).commitAllowingStateLoss();                break;            case R.id.navigation_menu2:                transaction.replace(R.id.frame_home, menu2Fragment).commitAllowingStateLoss();                break;            case R.id.navigation_menu3:                transaction.replace(R.id.frame_home, menu3Fragment).commitAllowingStateLoss();                break;            case R.id.navigation_menu4:                transaction.replace(R.id.frame_home, menu4Fragment).commitAllowingStateLoss();                break;        }        return true;    }}

翻阅古今

试试这段代码。&nbsp;return false;&nbsp;or&nbsp;&nbsp;return true;&nbsp; public boolean onNavigationItemSelected(@NonNull MenuItem item) {&nbsp; &nbsp; // uncheck the other items.&nbsp; &nbsp; mMenuId = item.getItemId();&nbsp; &nbsp; for (int i = 0; i < mBtmView.getMenu().size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; MenuItem menuItem = mBtmView.getMenu().getItem(i);&nbsp; &nbsp; &nbsp; &nbsp; boolean isChecked = menuItem.getItemId() == item.getItemId();&nbsp; &nbsp; &nbsp; &nbsp; menuItem.setChecked(isChecked);&nbsp; &nbsp; }&nbsp; &nbsp; switch (item.getItemId()) {&nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_food: {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_medical: {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_yoga: {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_postures: {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; return true;}
随时随地看视频慕课网APP

相关分类

Java
我要回答