如何在活动之间使用底部导航视图按下时突出显示项目?
2022-09-03 00:29:29
我已经为我的应用程序添加了底部导航视图,但我需要在活动之间而不是片段之间使用底部导航视图,因此我已将此代码添加到Java中,用于我的所有3个活动。
当我在手机中选择第二或第三时,所有事情都是正确的,但问题是突出显示到第一项。
我需要突出显示我按下的项目。
我已经使用了片段,它工作得很好,但我仍然是使用片段的初学者,所以我正在使用活动。
第一个活动代码是:
BottomNavigationView mBottomNavigation;
mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);
mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Nav_Second:
Intent Second= new Intent(First.this, Second.class);
startActivity(Second);
break;
case R.id.Nav_Third:
Intent Third= new Intent(First.this, Third.class);
startActivity(Third);
break;
}
return true;
}
});
}}
第二个活动是:
BottomNavigationView mBottomNavigation;
mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);
mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Nav_First:
Intent First= new Intent(Second.this, First.class);
startActivity(First);
break;
case R.id.Nav_Third:
Intent Third= new Intent(Second.this, Third.class);
startActivity(Third);
break;
}
return true;
}
});
}}
第三个活动是:
BottomNavigationView mBottomNavigation;
mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);
mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Nav_First:
Intent First= new Intent(Third.this, First.class);
startActivity(First);
break;
case R.id.Nav_Second:
Intent Second= new Intent(Third.this, Second.class);
startActivity(Second);
break;
}
return true;
}
});
}}
xml 对于 3 个活动是相同的:
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BottomNavigator"
android:background="@color/colorPrimaryDark"
android:layout_alignParentBottom="true"
app:itemTextColor="@drawable/item_bg"
app:itemIconTint="@drawable/item_bg"
app:menu="@menu/navigate_items">
</android.support.design.widget.BottomNavigationView>