如何在侧边栏导航中创建click事件 [英] how to create the click event in sidebar navigation

查看:87
本文介绍了如何在侧边栏导航中创建click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序中创建了侧边栏导航.现在,我要在其上创建一个click事件.

I have created the sidebar navigation in my app. Now I want to create a click event on that.

我已经搜索了它,并且得到了文档,但是我听不懂.

I have searched for it and I got the documentation but I'm unable to understand the same.

所以请任何人可以提出一个更好的来源,因为它会有所帮助. youtube上的教程会有所帮助.

so please anyone can suggest a better source for it will be helpful. tutorial on youtube will be helpful.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/nav_account"
        android:icon="@mipmap/ic_person_black_24dp"
        android:title="My Account"/>

    <item android:id="@+id/nav_setting"
        android:icon="@mipmap/ic_settings_black_24dp"
        android:title="Settings"/>

    <item android:id="@+id/nav_logout"
        android:icon="@mipmap/ic_logout_black_24dp"
        android:title="Logout"/>
    </menu>

我的activity_main.xml

<?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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.jdstudio.MainActivity">

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

        <include
            layout="@layout/navigation_action"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Main Layout"
            android:textAlignment="center"
            android:textSize="24dp" />
    </LinearLayout>

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

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

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

推荐答案

只需实现NavigationView.OnNavigationItemSelectedListener接口,然后重写方法onNavigationItemSelected(MenuItem menuItem),如下所示:

Simply implement NavigationView.OnNavigationItemSelectedListener interface and then override the method onNavigationItemSelected(MenuItem menuItem) like the following:

public class MainActivity extends AppCompatActivity implements
    NavigationView.OnNavigationItemSelectedListener{

    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // declaring the NavigationView
        navigationView = (NavigationView) findViewById(R.id.navigationView);
        // assigning the listener to the NavigationView
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {

        menuItem.setChecked(true);

        switch (menuItem.getItemId()) {
            case R.id.nav_account:
                // do you click actions for the first selection
                break;
            case R.id.nav_setting:
                // do you click actions for the second selection
                break;
            case R.id.nav_logout:
                // do you click actions for the third selection
                break;
        }

        return true;
    }

}

当然不要忘记在xml布局中将id赋予NavigationView:

And for sure don't forget to give id to the NavigationView in your xml-layout:

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

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

这篇关于如何在侧边栏导航中创建click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆