我正在创建一个布局,我想在同一布局中添加导航抽屉和底部导航,并在项目上单击。相应的片段将同时在导航抽屉和底部导航上打开,但是问题是当我包括底部导航项目时单击导航抽屉停止工作,并且在没有“底部导航”的情况下也可以单击项目。我不知道为什么会这样,我尽力解决了这个问题!如果你们能给我建议,那会有所帮助
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="Tara" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#780116"
android:theme="@style/NavigationTheme"
app:itemTextColor="#C0CCDA"
app:menu="@menu/drawer_menu_bk" />
<include layout="@layout/bottomnav"/>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.nav_drawer_open, R.string.nav_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
//when rotate device activity gets destroyed and recreated or change text size of the device
// or app gets killed cz f low memory to avoid we use this
if (savedInstanceState == null) {
//when app run first time or press back button n come back this fragment will show on screen to avoid empty activity
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new LibraryFragment()).commit();
navigationView.setCheckedItem(R.id.nav_fav);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_fav:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new LibraryFragment()).commit();
break;
case R.id.nav_downloads:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new DownloadsFragment()).commit();
break;
case R.id.nav_share:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MyVideosFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}