如何实现onNavigationItemSelected方法?

How to implement the method onNavigationItemSelected?

我创建了一个导航抽屉activity。我想创建一个片段,在单击抽屉列表中的相应项目时打开该片段。

我该如何实现才能在点击相应项目时显示图像?

让我分享一个简单而完整的代码来使用 Navigation Drawer。 MainActivity class.

public class MainActivity extends AppCompatActivity implements
    NavigationView.OnNavigationItemSelectedListener{

private NavigationView navigationView;
private DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(this);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle =
            new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) {
                @Override
                public void onDrawerClosed(View drawerView){
                    super.onDrawerClosed(drawerView);
                }

                @Override
                public void onDrawerOpened(View drawerView){
                    super.onDrawerOpened(drawerView);
                }
            };

    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    actionBarDrawerToggle.syncState();

}

@Override
public boolean onNavigationItemSelected(MenuItem item){

    int id = item.getItemId();

    switch (id){
        case R.id.item1:
           /* getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new MySimpleFragment())
                    .addToBackStack(null)
                    .commit();*/
            Intent intent = new Intent(this, Class1.class);
            startActivity(intent);
            break;
        case R.id.item2:
            intent = new Intent(this, Class2.class);
            startActivity(intent);
            break;
        case R.id.item3:
            intent = new Intent(this, Class3.class);
            startActivity(intent);
            break;
        default:
            break;
    }
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

}

activity_main.xml 文件如下所示:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<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"
    android:weightSum="1"
    android:padding="3dp"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize"
        android:background="@android:color/transparent"/>

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_below="@id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        tools:context=".MainActivity"
        tools:ignore="MergeRootFrame"
        android:background="@drawable/jackson">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textStyle="italic"
            android:padding="30dp"
            android:layout_marginLeft="50dp"
            android:textSize="15dp"
            android:layout_marginTop="40dp"
            android:textColor="@color/red"
            android:text="Just Beat It"/>
    </FrameLayout>

</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@drawable/drawer"
    app:itemIconTint="@color/colorAccent"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer">
</android.support.design.widget.NavigationView>

而且,header.xml 文件的布局是

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="190dp"
    android:background="@drawable/header"
    android:orientation="vertical">

    <ImageView
        android:layout_width="76dp"
        android:layout_height="76dp"
        android:id="@+id/profile_image"
        android:layout_marginLeft="54dp"
        android:layout_marginTop="50dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/myphoto_cutmypic"/>

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/profile_image"
        android:layout_marginLeft="54dp"
        android:textStyle="bold"
        android:paddingTop="10dp"
        android:textColor="@color/red"
        android:text="@string/my_name"/>
    <TextView
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt1"
        android:textStyle="bold"
        android:paddingTop="10dp"
        android:layout_marginLeft="54dp"
        android:textColor="@color/red"
        android:text="@string/my_email"/>

</RelativeLayout>

而且,menu/drawer.xml 文件看起来像:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/item1"
            android:checked="false"
            android:icon="@drawable/ic_call1"
            android:title="item1"/>

        <item
            android:id="@+id/item2"
            android:checked="false"
            android:icon="@drawable/ic_call2"
            android:title="item2"/>

        <item
            android:id="@+id/item3"
            android:checked="false"
            android:icon="@drawable/ic_call3"
            android:title="item3"/>

    </group>
</menu>

如有疑问,请回复本回答