我做了一个空的活动,然后将其更改为DrawerLayout,每次打开菜单并单击抽屉正在关闭的项目时,都不会发生任何事情,就像我只是打开和关闭它一样,无法执行其他任何操作。
这是布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/navigation_home_drawer">
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/mainmenu"
>
</com.google.android.material.navigation.NavigationView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
这是Java代码
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = findViewById(R.id.navigation_home_drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView =findViewById(R.id.navigation);
navigationView.setNavigationItemSelectedListener(this);
mMapView = findViewById(R.id.map);
initGoogleMap(savedInstanceState);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_home:{
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();
break;
}
case R.id.nav_profile: {
Intent i = new Intent(this, ProfileActivity.class);
startActivity(i);
finish();
break;
}
case R.id.nav_settings: {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
finish();
break;
}
}
return true;
}
}
这里是菜单布局
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:title="Home"
android:icon="@drawable/ic_home_black_24dp">
</item>
<item
android:id="@+id/nav_profile"
android:title="My Profile"
android:icon="@drawable/ic_person_black_24dp">
</item>
<item
android:id="@+id/nav_settings"
android:title="Settings"
android:icon="@drawable/ic_settigs">
</item>
</menu>
我在此文件中也有Map的代码,但这并不重要,当我从菜单中选择一个项目时,我只想将活动更改为该项目,但是什么也没有发生,只关闭导航,请您告诉我如果我忘记添加某些内容,或者应该进行哪些更改才能使导航正常工作?预先谢谢!
您可以使用此代码制作导航抽屉: