我是Android开发的新手,我正在尝试使用bottomNavigationView创建一个应用程序。我创建了一个单独的类来为不同的活动设置bottomNavigationView,这样我就不必一次又一次地编写代码。但是,当我在Android设备上启动该应用程序时,无论我单击哪个项目,它都将再次启动“欢迎”活动。这是我的navigationHelperClass
public class BottomNavigationViewHelper {
private static final String TAG = "BottomNavigationViewHel";
public static void setUpNavigationView(BottomNavigationViewEx bottomNavigationViewEx){
Log.d(TAG, "setUpNavigationView: setting BottomNavigation");
bottomNavigationViewEx.enableAnimation(false);
bottomNavigationViewEx.enableItemShiftingMode(false);
bottomNavigationViewEx.enableShiftingMode(false);
bottomNavigationViewEx.setTextVisibility(false);
}
public static void enableNavigation(final Context context, final BottomNavigationViewEx viewEx){
viewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.btnHome:
viewEx.setSelectedItemId(R.id.btnHome);
Intent intent = new Intent(context, Welcome.class);
context.startActivity(intent);
break;
case R.id.btnSearch:
Intent intent1 = new Intent(context, Chats.class);
context.startActivity(intent1);
break;
case R.id.btnPost:
Intent intent2 = new Intent(context, Posts.class);
context.startActivity(intent2);
break;
case R.id.btnFavourites:
Intent intent3 = new Intent(context, Favourites.class);
context.startActivity(intent3);
break;
case R.id.btnProfile:
Intent intent4 = new Intent(context, Profile.class);
context.startActivity(intent4);
break;
}
return false;
}
});
}
}
这是我的“欢迎”活动,当我单击bottomNavigationView的任何一项时,就会开始。
公共类Welcome扩展AppCompatActivity { 私有上下文mCntext = Welcome.this;
private static final String TAG = "Welcome";
BottomNavigationViewEx bottomNav;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Log.d(TAG, "onCreate: starting");
setupBottomNavigationView();
Menu menu = bottomNav.getMenu();
MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
private void setupBottomNavigationView(){
Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
bottomNav = findViewById(R.id.nav_bottom);
BottomNavigationViewHelper.setUpNavigationView(bottomNav);
BottomNavigationViewHelper.enableNavigation(mCntext, bottomNav);
}
}
这是我进行的一项活动,其余活动的代码也相同。这是个人资料活动。
public class Profile extends AppCompatActivity {
private Context mContext = Profile.this;
private static final String TAG = "Search";
BottomNavigationViewEx bottomNav;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
setupBottomNavigationView();
}
private void setupBottomNavigationView(){
Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
bottomNav = findViewById(R.id.nav_bottom);
BottomNavigationViewHelper.setUpNavigationView(bottomNav);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNav);
Menu menu = bottomNav.getMenu();
MenuItem menuItem = menu.getItem(4);
menuItem.setChecked(true);
}
@Override
public void setTitle(CharSequence title) {
}
}
答案 0 :(得分:1)
删除以下行:
viewEx.setSelectedItemId(R.id.btnHome);
从case R.id.btnHome:
回调下的案例onNavigationItemSelected
开始。
答案 1 :(得分:0)
请勿在活动中使用底部导航,而应使用片段。创建一个将保留您的片段的父活动,然后在onNavigationItemSelectListener中更改这些片段,在这种情况下,您不必管理底部导航状态和所选项目。您可以在此处查看如何更改片段How to change fragment with the Bottom Navigation Activity?