我是这个NavigationDrawer概念的新手,我没有使用MainActivity中的工具栏。我使用内置的Appbar来添加导航选项并将项目添加到其中。现在,我想知道如何使该项目可点击,并且通过单击它们,它应该进入另一项活动。
主要活动 :

public class Main2Activity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.drawer_open,R.string.drawer_close);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mToggle.onOptionsItemSelected(item)){

            return  true;

        }

        return super.onOptionsItemSelected(item);
    }
}


activity_main.xml:

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xyz.navigationdemo.Main2Activity"
android:id="@+id/drawerLayout">


<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2">

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/viewPager">

        </android.support.v4.view.ViewPager>

    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="15dp">


        <ImageView

            android:id="@+id/dua"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1"

            android:src="@drawable/bpone"
            tools:ignore="ContentDescription,NestedWeights,RtlHardcoded" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/bpone"
            android:id="@+id/time"
            android:clickable="true"
            android:layout_marginRight="5dp"
            tools:ignore="ContentDescription,RtlHardcoded" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="true"
            android:id="@+id/gall"
            android:src="@drawable/bpone"
            tools:ignore="ContentDescription" />



    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="10dp">
        <ImageView
            android:layout_width="0dp"
            android:id="@+id/speech"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:clickable="true"
            android:src="@drawable/bpone"
            android:layout_marginRight="5dp"
            tools:ignore="ContentDescription,NestedWeights,RtlHardcoded" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/batch"
            android:clickable="true"
            android:src="@drawable/bpone"
            android:layout_marginRight="5dp"
            tools:ignore="ContentDescription,RtlHardcoded" />

        <ImageView
            android:layout_width="0dp"
            android:id="@+id/about"
            android:clickable="true"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/bpone"
            tools:ignore="ContentDescription" />

    </LinearLayout>



</LinearLayout>


<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:background="#B3E5FC"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/navigation_menu"
    app:headerLayout="@layout/navigation_header">


</android.support.design.widget.NavigationView>



navigation_menu.xml:




<item android:id="@+id/nav_account"
      android:title="@string/alqalam"
    android:icon="@drawable/home"/>

<item android:id="@+id/nav_feedback"
    android:title="@string/feedback"
    android:icon="@drawable/feedback"/>

<item android:id="@+id/nav_login"
    android:title="@string/login"
    android:icon="@drawable/login"/>

<item android:id="@+id/nav_aboutus"
    android:title="@string/aboutus"
    android:icon="@drawable/aboutus"/>

<item android:id="@+id/nav_share"
    android:title="@string/share"
    android:icon="@drawable/share"/>

最佳答案

尝试这个

navigationView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {

  @Override
  public boolean onNavigationItemSelected(MenuItem item) {

   int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
        i=new Intent(youractivity.this,youractivity);
        startActivity(i);
    } else if (id == R.id.nav_gallery) {
        i=new Intent(youractivity.this,youractivity);
        startActivity(i);
    } else if (id == R.id.nav_slideshow) {
        //same
    } else if (id == R.id.nav_manage) {
        //same
    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

 return true;
            }

        });

关于java - 如何仅在 Activity 类中使NavigationDrawer项成为可单击项,并且应导航到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45211532/

10-10 16:03