BottomNavigationView 的菜单未选中
BottomNavigationView's menu don't selected
我刚刚单独创建 BottomNavigationView 时效果很好。但是将 BottomNavigationView 与 Fragment 一起使用有问题。问题是 Fragment 变化很好,但 BottomNavigationView 菜单固定在第一个菜单上。
我需要在哪里设置encoding/charsets?
(抱歉我的英语不好...)
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:
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 false;
}
}
尝试像这样在最后返回 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;
}
}
试试这个代码。
return false;
or
return true;
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// uncheck the other items.
mMenuId = item.getItemId();
for (int i = 0; i < mBtmView.getMenu().size(); i++) {
MenuItem menuItem = mBtmView.getMenu().getItem(i);
boolean isChecked = menuItem.getItemId() == item.getItemId();
menuItem.setChecked(isChecked);
}
switch (item.getItemId()) {
case R.id.action_food: {
}
break;
case R.id.action_medical: {
}
break;
case R.id.action_yoga: {
}
break;
case R.id.action_postures: {
}
break;
}
return true;
}
我刚刚单独创建 BottomNavigationView 时效果很好。但是将 BottomNavigationView 与 Fragment 一起使用有问题。问题是 Fragment 变化很好,但 BottomNavigationView 菜单固定在第一个菜单上。
我需要在哪里设置encoding/charsets? (抱歉我的英语不好...)
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:
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 false;
}
}
尝试像这样在最后返回 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;
}
}
试试这个代码。
return false;
or
return true;
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// uncheck the other items.
mMenuId = item.getItemId();
for (int i = 0; i < mBtmView.getMenu().size(); i++) {
MenuItem menuItem = mBtmView.getMenu().getItem(i);
boolean isChecked = menuItem.getItemId() == item.getItemId();
menuItem.setChecked(isChecked);
}
switch (item.getItemId()) {
case R.id.action_food: {
}
break;
case R.id.action_medical: {
}
break;
case R.id.action_yoga: {
}
break;
case R.id.action_postures: {
}
break;
}
return true;
}