我遇到的问题与here之前的问题类似,但未得到答复。
我不能在同一活动中使用NavigationView
和BottomNavigationView
。
我在menu.xml
文件夹中有两个menu
,并给menu.xml
和NavigationView
赋予了不同的BottomNavigationView
。
NavigationView
点击监听器正在工作。
BottomNavigationView
点击监听器不起作用。即使单击其他选项卡,BottomNavigationView
的第一个选项卡也始终处于选中状态。
请尽快帮助我。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--Bottom Navigation: show menu-->
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_menu" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/side_nav"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
ads:menu="@menu/side_menu"
app:headerLayout="@layout/nav_header" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mSideNav;
ActionBarDrawerToggle mToggle;
BottomNavigationView mBottomNav;
private FrameLayout mFrmLayout;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = findViewById(R.id.drawableLayout);
mBottomNav = findViewById(R.id.bottom_nav);
mSideNav = findViewById(R.id.side_nav);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.setDrawerListener(mToggle);
mToggle.syncState();
//bottom navigation
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_home:
//home fragment transaction
actionBar.setTitle("Home");
Toast.makeText(MainActivity.this, "Home", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_downloading:
//profile fragment transaction
actionBar.setTitle("Downloading");//change actionbar title
Toast.makeText(MainActivity.this, "Downloading", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_completed:
//users fragment transaction
actionBar.setTitle("Completed");//change actionbar title
Toast.makeText(MainActivity.this, "Completed", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
//side navigation
mSideNav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
// Handle mNavgnView view item clicks here.
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_home:
actionBar.setTitle("Home");
Toast.makeText(MainActivity.this, "Home", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_downloading:
actionBar.setTitle("Downloading");
Toast.makeText(MainActivity.this, "Downloading", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_completed:
actionBar.setTitle("Completed");
Toast.makeText(MainActivity.this, "Completed", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_bookmarks:
actionBar.setTitle("Bookmarks");
Toast.makeText(MainActivity.this, "Bookmarks", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_history:
actionBar.setTitle("History");
Toast.makeText(MainActivity.this, "History", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_about:
actionBar.setTitle("About");
Toast.makeText(MainActivity.this, "About", Toast.LENGTH_SHORT).show();
break;
}
DrawerLayout drawer = findViewById(R.id.drawableLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
actionBar.setTitle("Home");
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawableLayout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
} else {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
return super.onOptionsItemSelected(item);
}
}