Java 在活动之间使用 BottomNavigationView 按下时如何突出显示项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41744219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to highlight the item when pressed using BottomNavigationView between activities?
提问by 7uthaifah
I have added Bottom Navigation View for my app but I need the Bottom Navigation View between activities instead of fragment so I have added this code to Java for all my 3 activities.
我为我的应用程序添加了底部导航视图,但我需要活动之间的底部导航视图而不是片段,因此我已将此代码添加到 Java 中以用于我的所有 3 个活动。
When I select Second or Third in my phone all things are correct but the problem is the highlight goes to the First item.
当我在手机中选择第二个或第三个时,一切都是正确的,但问题是高亮显示在第一个项目上。
I need to highlight the item I press.
我需要突出显示我按下的项目。
I have used fragment and it works perfectly but I am still beginner for using fragment so I am using activities.
我已经使用了片段并且它工作得很好,但我仍然是使用片段的初学者,所以我正在使用活动。
The first activity code is:
第一个活动代码是:
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;
}
});
}}
The second activity is:
第二个活动是:
BottomNavigationView mBottomNavigation;
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;
}
});
}}
The third activity is:
第三个活动是:
BottomNavigationView mBottomNavigation;
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;
}
});
}}
The xml are the same for 3 activities:
3 个活动的 xml 相同:
<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>
回答by javaxian
You can use BottomNavigationView with activities representing tabs. The key is to repeat the navigation view component in each activity, and have the code of each activity control the navigation component: starting the right activity on navigation item clicks and selecting a proper navigation item after the activity has started.
您可以将 BottomNavigationView 与表示选项卡的活动一起使用。关键是在每个活动中重复导航视图组件,并让每个活动的代码控制导航组件:在导航项点击时启动正确的活动,并在活动启动后选择合适的导航项。
You need to start newly selected activities (tabs) with a delay as to allow the tab switching animation to complete before the new activity replaces the previous one.
您需要延迟启动新选择的活动(选项卡),以便在新活动替换前一个活动之前完成选项卡切换动画。
My approach was to have all activities representing tabs inherit from the same BaseActivity class implementing the common behavior.
我的方法是让所有表示选项卡的活动从实现通用行为的同一个 BaseActivity 类继承。
This is the code of an example BaseActivity:
这是示例 BaseActivity 的代码:
public abstract class BaseActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
protected BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
navigationView = (BottomNavigationView) findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(this);
}
@Override
protected void onStart() {
super.onStart();
updateNavigationBarState();
}
// Remove inter-activity transition to avoid screen tossing on tapping bottom navigation items
@Override
public void onPause() {
super.onPause();
overridePendingTransition(0, 0);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
navigationView.postDelayed(() -> {
int itemId = item.getItemId();
if (itemId == R.id.navigation_home) {
startActivity(new Intent(this, HomeActivity.class));
} else if (itemId == R.id.navigation_dashboard) {
startActivity(new Intent(this, DashboardActivity.class));
} else if (itemId == R.id.navigation_notifications) {
startActivity(new Intent(this, NotificationsActivity.class));
}
finish();
}, 300);
return true;
}
private void updateNavigationBarState(){
int actionId = getNavigationMenuItemId();
selectBottomNavigationBarItem(actionId);
}
void selectBottomNavigationBarItem(int itemId) {
Menu menu = navigationView.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem item = menu.getItem(i);
boolean shouldBeChecked = item.getItemId() == itemId;
if (shouldBeChecked) {
item.setChecked(true);
break;
}
}
}
abstract int getContentViewId();
abstract int getNavigationMenuItemId();
}
This is my whole example based on the Android Studio's Bottom Navigation Activity template:
这是我基于 Android Studio 的底部导航活动模板的整个示例:
https://github.com/ddekanski/BottomNavigationViewBetweenActivities
https://github.com/ddekanski/BottomNavigationViewBetweenActivities
回答by hardanger
To anyone still looking for this, @javazian's answer of extending each activity is real overkill in my opinion.
对于任何仍在寻找这个的人来说,@javazian 扩展每项活动的答案在我看来真的是矫枉过正。
A much more succinct solution is to retrieve the nav menu and manually check the relevant menu item.
一个更简洁的解决方案是检索导航菜单并手动检查相关菜单项。
Note in the example below, INSERT_INDEX_HERE needs to be replaced with the index of the menu item, (e.g. the menu item on the far left would have index 0).
请注意,在下面的示例中,INSERT_INDEX_HERE 需要替换为菜单项的索引(例如,最左侧的菜单项的索引为 0)。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connections);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
// Ensure correct menu item is selected (where the magic happens)
Menu menu = navigation.getMenu();
MenuItem menuItem = menu.getItem(INSERT_INDEX_HERE);
menuItem.setChecked(true);
}
回答by Vlad Sonkin
You absolutely can use BottomNavigationView with activities, but it's not recommended. Better is to use it with Fragments, main benefits are:
您绝对可以将 BottomNavigationView 与活动一起使用,但不建议这样做。更好的是将它与 Fragments 一起使用,主要好处是:
- Animation in BottomNavigationView (without ugly delay in case of Activities)
- Maintain state of Fragments easily
- Flexible backstack
- BottomNavigationView 中的动画(在活动的情况下没有丑陋的延迟)
- 轻松维护 Fragment 的状态
- 灵活的后台
But if you want to stick with Activities, then @javaxian answer is very good, but we can improve it:
但是如果你想坚持使用活动,那么@javaxian 的回答非常好,但我们可以改进它:
- If our layout file doesn't have a BottomNavigation with proper id, then we will have exception. Better is to have common layout file for activities with navigation.
- 如果我们的布局文件没有具有正确 id 的 BottomNavigation,那么我们将有异常。更好的是为带有导航的活动提供通用的布局文件。
Solution:
解决方案:
1) Rename BaseActivity to BaseNavigationActivity
1) 将 BaseActivity 重命名为 BaseNavigationActivity
2) Make single layout file for all your Activities (for example activity_navigation.xml
), and explicitly call in BaseNavigationActivity setContentView(R.layout.activity_navigation);
2)为您的所有活动(例如)制作单个布局文件activity_navigation.xml
,并显式调用 BaseNavigationActivitysetContentView(R.layout.activity_navigation);
3) Remove abstract int getContentViewId();
3) 删除 abstract int getContentViewId();
- Code in
selectBottomNavigationBarItem(int itemId)
is overhead, we can do it much more simplier
- 代码输入
selectBottomNavigationBarItem(int itemId)
是开销,我们可以做得更简单
Solution(in Kotlin):
解决方案(在 Kotlin 中):
private fun selectBottomNavigationBarItem(itemId: Int) {
val menuItem = navigationView.menu.findItem(itemId)
menuItem.isChecked = true
}
回答by Oyeme
You can use setSelectedItemIdin each activity when it starts.
您可以在每个活动启动时使用setSelectedItemId。
bottomNavigationView.setSelectedItemId(R.id.menu_item_id);
where R.id.menu_item_idlocated in menu/navigate_items
其中R.id.menu_item_id位于 menu/navigate_items