我用onNavigationItemSelected实现了导航抽屉。 这是我的主要行动:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.dl);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
//noinspection RestrictedApi
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.nv);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.masar)
startActivity(new Intent(this, Masarat.class));
else if (id == R.id.company)
startActivity(new Intent(this, Masarat.class));
else if (id == R.id.profile)
startActivity(new Intent(this, Masarat.class));
else if (id == R.id.logout)
startActivity(new Intent(this, Masarat.class));
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
包含导航抽屉之外的Home UI元素的Activity的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dl"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<android.support.design.widget.NavigationView
android:id="@+id/nv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/nav_said"
app:headerLayout="@layout/header"
android:layout_gravity="start"
>
</android.support.design.widget.NavigationView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/bg">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/profiletransparent"
android:scaleType="fitXY"
/>
<ImageView
android:id="@+id/logo"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/yo"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<TextView
android:layout_below="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3rfne"
android:textSize="40dp"
android:textColor="#fff"
android:gravity="center"
android:layout_marginTop="70dp"
android:textStyle="normal"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
当点击项目时,它应该转到其他活动,但它不起作用(当我点击项目时导航抽屉正在关闭)
问题是什么?