Android 导航抽屉中的onClick事件

Android 导航抽屉中的onClick事件,android,navigation-drawer,Android,Navigation Drawer,我在Android中制作了一个导航抽屉,我想在其中实现onClick。这是我的主要活动: public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle aToggle; private Toolbar toolbar; private RecyclerView recyclerView; private Recycle

我在Android中制作了一个导航抽屉,我想在其中实现onClick。这是我的主要活动:

public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle aToggle;
private Toolbar toolbar;
private RecyclerView recyclerView;
private RecyclerAdapter recyclerAdapter;
private RecyclerView.Adapter adapter;
private NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    aToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.navig, R.string.open, R.string.Close);
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    mDrawerLayout.addDrawerListener(aToggle);
    toolbar = (Toolbar) findViewById(R.id.nav_action);
    toolbar.setNavigationIcon(R.drawable.navig);
    setSupportActionBar(toolbar);
    aToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    navigationView.setItemIconTintList(null);
    recyclerView = (RecyclerView) findViewById(R.id.recycler);
    recyclerAdapter = new RecyclerAdapter(getApplicationContext());
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(recyclerAdapter);


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (aToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);

}}
 <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.alpit.formula2.MainActivity">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="0dp"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="58dp"
            android:orientation="vertical"></android.support.v7.widget.RecyclerView>

        <android.support.v7.widget.Toolbar
            android:id="@+id/nav_action"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#EF6C00"
            android:orientation="vertical"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"></android.support.v7.widget.Toolbar>


    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#FFA726"
        app:menu="@menu/navigation_menu"
        app:theme="@style/NavigationTheme">


    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是我的活动XML布局:

public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle aToggle;
private Toolbar toolbar;
private RecyclerView recyclerView;
private RecyclerAdapter recyclerAdapter;
private RecyclerView.Adapter adapter;
private NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    aToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.navig, R.string.open, R.string.Close);
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    mDrawerLayout.addDrawerListener(aToggle);
    toolbar = (Toolbar) findViewById(R.id.nav_action);
    toolbar.setNavigationIcon(R.drawable.navig);
    setSupportActionBar(toolbar);
    aToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    navigationView.setItemIconTintList(null);
    recyclerView = (RecyclerView) findViewById(R.id.recycler);
    recyclerAdapter = new RecyclerAdapter(getApplicationContext());
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(recyclerAdapter);


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (aToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);

}}
 <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.alpit.formula2.MainActivity">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="0dp"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="58dp"
            android:orientation="vertical"></android.support.v7.widget.RecyclerView>

        <android.support.v7.widget.Toolbar
            android:id="@+id/nav_action"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#EF6C00"
            android:orientation="vertical"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"></android.support.v7.widget.Toolbar>


    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#FFA726"
        app:menu="@menu/navigation_menu"
        app:theme="@style/NavigationTheme">


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

这是我的菜单项:


问题是我无法理解如何在导航抽屉上实现
onClick
,因为它是由我们提供的列表而不是任何listView填充的

如何在导航抽屉的项目上单击
onClick

您需要添加实现导航视图。OnNavigationItemSelectedListener

private void setNavigationViewListener() {
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}
和添加方法

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    switch (item.getItemId()) {

       case R.id.nav_maths: {
      //do somthing
            break;
        }  
    }
    //close navigation drawer
    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;
}
设置侦听器的方法

private void setNavigationViewListener() {
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}
在onCreate()中调用方法

下面的代码用于将切换添加到抽屉布局

和将侦听器添加到导航菜单项

implements NavigationView.OnNavigationItemSelectedListener
并重写下面的方法

@Override
  public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    if(id == R.id. nav_maths){
      //Handle your stuff here
    } 
}
将下面的代码添加到onCreate方法中


将导航项SelectedListener添加到导航视图

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
使用NavigationView.OnNavigationItemSelectedListener实施您的活动

private void setNavigationViewListener() {
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}
并为单击项添加此代码

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

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

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

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

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

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
在你的

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (aToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);

}}
添加以下行:

switch (item.getItemId()) {
    case R.id.nav_maths:
        // your logic here.
        return true;
    case R.id.nav_physics:
        //your logic here
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

步骤1:扩展活动的
导航视图.OnNavigationItemSelectedListener
第二步: 然后将显示一个错误,然后覆盖该方法:

  @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        return false;
    }
使用
menuItem.getId()
获取Id,并根据需要执行操作

最后一步:在onCreate
navigationView.setNavigationItemSelectedListener(此)中添加此行

就这样

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val nav_view: NavigationView = findViewById(R.id.nav_view)
    nav_view.setNavigationItemSelectedListener(this)
    nav_view.bringToFront();
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when(item.itemId){
        R.id.miid_log_out -> Toast.makeText(this, "Working", Toast.LENGTH_SHORT).show()
    }
    return false
}

}

因为它是由我们提供的列表而不是任何列表视图填充的
您能解释一下这是什么意思吗?很抱歉,我知道这一行会有bug,在实现导航视图后,我扫描了许多关于它们的教程,它们都由列表视图填充,因为它们可以很容易地点击项目的位置。这就是我添加这一行的原因。我没有使用framgents,所以我必须在每个活动中实现此方法,或者有其他方法。谢谢。是的,您必须为每个其他活动创建导航视图并将其写入。但这不是最佳实践。使用fragment这将有助于你完成任务抱歉,回复太晚,但我这样做时什么都没有发生。我这样做时什么都没有发生。你错过了返回布尔值和关闭抽屉。你能告诉我如何在此实现onClickListner吗?它在我的项目中更有帮助,工作正常。谢谢。如果点击实际上是在菜单的标题上呢?我正在尝试捕获标题中的头像点击,而不是菜单项。不知何故,即使添加了所有代码,它也不会调用所选的项目。请添加一些上下文,以了解此代码的工作原理。这将提高您答案的质量!:)虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
  @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        return false;
    }
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val nav_view: NavigationView = findViewById(R.id.nav_view)
    nav_view.setNavigationItemSelectedListener(this)
    nav_view.bringToFront();
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when(item.itemId){
        R.id.miid_log_out -> Toast.makeText(this, "Working", Toast.LENGTH_SHORT).show()
    }
    return false
}
 binding.navigationView.setNavigationItemSelectedListener {
            when(it.itemId){
                R.id.book -> Toast.makeText(applicationContext, "Booked", Toast.LENGTH_SHORT).show()
            }
            true
        }