我似乎无法想出这个。我有MainActivity并创建了我希望能够导航到的SecondActivity和ThirdActivity。
我在MainActivity中使用BottomNavigation在活动之间导航:
public class MainActivity extends AppCompatActivity {
protected BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_menuItem1:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
return true;
case R.id.navigation_menuItem2:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
return true;
case R.id.navigation_menuItem3:
Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
startActivity(intent);
return true;
}
return false;
}
};
navigationView = (BottomNavigationView) findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
有什么理由不切换页面/活动?
编辑:添加以下行以使其正常工作:
protected BottomNavigationView navigationView;
和
navigationView = (BottomNavigationView) findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
感谢您的帮助!
答案 0 :(得分:1)
万一你错过了什么,请确保你没有忘记:
在布局的xml中创建BottomNavigationView:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
在菜单资源文件夹中的navigation.xml中创建一个文件。此文件用于在BottomNavigationView
中提供MenuItems<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_menuItem1"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/menuItem1" />
<item
android:id="@+id/navigation_menuItem2"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/menuItem2" />
<item
android:id="@+id/navigation_menuItem3"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/menuItem3" />
</menu>
现在让我们在菜单项上设置Click Events OnNavigationItemSelectedListener和OnNavigationItemReselectedListener的监听器:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_menuItem1:
return true;
case R.id.navigation_menuItem2:
return true;
case R.id.navigation_menuItem3:
return true;
}
return true;
}
};
private BottomNavigationView.OnNavigationItemReselectedListener mOnNavigationItemReselectedListener = new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_menuItem1:
Log.d(TAG, "navigation_menuItem1 Reselected ===");
break;
case R.id.navigation_menuItem2:
Log.d(TAG, "navigation_menuItem2 Reselected ===");
break;
case R.id.navigation_menuItem3:
Log.d(TAG, "navigation_menuItem3 Reselected ===");
break;
}
}
};
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
bottomNavigationView.setOnNavigationItemReselectedListener(mOnNavigationItemReselectedListener);
修改强>
将其添加到您的onCreate()
BottomNavigationView bottomNavigationView;
bottomNavigationView = findViewById(R.id.navigation);